将数组数组转换为 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 是:
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"]}
]
}
levelBricks int 有问题,并且没有从 json 正确加载任何内容.我究竟做错了什么?Debug.Log 给出一个错误,如果我只有 Debug.LoglevelBricks",我得到的只是Levels".我希望能够访问 Level1 和 Level2 中的数组.
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.
推荐答案
主要问题是您的类变量应该使用与您的 json 相同的名称.例子:公共字符串[]行;
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;
}
和getter/setter
and getter/setter
public static ClassName SaveFromString(string
jsonString) {
return JsonUtility.FromJson<ClassName>(jsonString);
}
public string SaveToString() {
return JsonUtility.ToJson(this);
}
和一个可序列化的类ClassForLevel12与 2
and a serializable class ClassForLevel12 with 2
public string[] level1;
public string[] level2;
(和其他类一样).
最终编辑.好的,这就是你所需要的,只需复制、粘贴和理解:
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);
}
}
二级
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]);
这篇关于将数组数组转换为 json的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!