Converting arrays of arrays to json

2020-02-02 03:26发布

问题:

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

string filename = "data.json";
string jsonString;
string path;

[System.Serializable]
public class LevelGrid
{
    public string[] rows;
}

[System.Serializable]
public class Levels
{
    public LevelGrid[] levels;
}

void LoadGrid()
{
    // Load data
    string path = Application.streamingAssetsPath + "/" + filename;
    jsonString = File.ReadAllText(path);
    var levelBricks = JsonUtility.FromJson<Levels>(jsonString);

    // DOESNT WORK
    Debug.Log(levelBricks.levels[0]); 

}

My data.json is:

 {
     "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"]}
     ]
 }

Something is wrong with the levelBricks int and nothing is properly being loaded from json. What am I doing wrong? The Debug.Log gives an error and if I only Debug.Log "levelBricks", all I get is "Levels". I'd like to be able to access the arrays in Level1 and Level2.

回答1:

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]);