The main problem is that your class variables should use the same name of your json.
example:
public string[] rows;
should be
public string[] Levels;
EDIT.
you need a serializable class with
[SerializeField] ClassForLevel12[] levels;
public ClassName() {
}
public ClassName(ClassForLevel12[] levels) {
this.levels = levels;
}
and getter/setter
public static ClassName SaveFromString(string
jsonString) {
return JsonUtility.FromJson<ClassName>(jsonString);
}
public string SaveToString() {
return JsonUtility.ToJson(this);
}
and a serializable class
ClassForLevel12
with 2
public string[] level1;
public string[] level2;
(and everything like the other class).
Final EDIT.
Ok this is what you need, just copy, paste and understand:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
[System.Serializable]
public class MyLevels{
[SerializeField] List<LevelGrid> levels;
public MyLevels(){}
public MyLevels(List<LevelGrid> levels){
this.levels = levels;
}
public List<LevelGrid> Levels{
get { return levels; }
set { levels = value; }
}
//from a jsonstring to an object
public static MyLevels SaveFromString(string jsonString){
return JsonUtility.FromJson<MyLevels>(jsonString);
}
//object to json
public string SaveToString(){
return JsonUtility.ToJson(this);
}
}
Second class
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
[System.Serializable]
public class LevelGrid{
[SerializeField] string[] level1;
[SerializeField] string[] level2;
public LevelGrid() {
}
public LevelGrid(string[] level1, string[] level2) {
this.level1 = level1;
this.level2 = level2;
}
public string[] Level1{
get { return level1; }
set { level1 = value; }
}
public string[] Level2{
get { return level2; }
set { level2 = value; }
}
//from a jsonstring to an object
public static LevelGrid SaveFromString(string jsonString) {
return JsonUtility.FromJson<LevelGrid>(jsonString);
}
//object to json
public string SaveToString() {
return JsonUtility.ToJson(this);
}
}
Now you can just call
string jsonString = "{ \"levels\": [ {\"level1\": [\"1,1,1,1,1\", \"1,1,1,1,1\", \"1,1,1,1,1\"]},{\"level2\":[\"1,1,1,1,1\", \"1,1,1,1,1\", \"1,0,1,0,1\"]}]}";
MyLevels levelBricks = MyLevels.SaveFromString(jsonString);
Debug.Log(levelBricks.Levels[0].Level1[0]);