繁体   English   中英

将数组数组转换为json

[英]Converting arrays of arrays to json

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

}

我的data.json是:

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

levelBricks int出了点问题,无法从json正确加载任何内容。 我究竟做错了什么? Debug.Log给出了一个错误,如果我仅使用Debug.Log“ levelBricks”,则得到的只是“ Levels”。 我希望能够访问Level1和Level2中的数组。

主要问题是您的类变量应使用与json相同的名称。 示例:public string []行;

应该是public string []级别;

编辑。 你需要一个可序列化的类

    [SerializeField] ClassForLevel12[] levels;

    public ClassName() {
    }

    public ClassName(ClassForLevel12[] levels) {
      this.levels = levels;
    }

和getter / setter

    public static ClassName SaveFromString(string 
jsonString) {
          return JsonUtility.FromJson<ClassName>(jsonString);
         }

    public string SaveToString() {
      return JsonUtility.ToJson(this);
    }

和一个带有2的可序列化类ClassForLevel12

    public string[] level1;
    public string[] level2;

(以及其他类的所有内容)。

最终编辑。 好的,这就是您所需要的,只需复制,粘贴并理解:

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

}

二等

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

现在您可以打电话

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM