将数组数组转换为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.Log"levelBricks",则得到的只是"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;
编辑. 您需要使用
[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;
(和其他类一样的东西).
(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);
}
}
第二堂课
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屋!