我有一个由两部分组成的问题。 首先,我尝试编写可以编写如下所示的 json 文件的代码。
{
"Restaurants":[
{
"Name" : "Asami",
"ID" : 0,
"Plates" :
[
{
"title" : "Plate1 ",
"color" : "Red ",
"price" : 5
},
{
"title" : "Plate1 ",
"color" : "Green ",
"price" : 6
},
{
"title" : "Plate1 ",
"color" : "Blue ",
"price" : 7
},
{
"title" : "Plate1 ",
"color" : "Yellow ",
"price" : 8
}
]
}
]
}
到目前为止我拥有的代码是这样的。
using System;
using System.Collections;
using System.IO;
using UnityEngine;
using System.Collections.Generic;
public class UnityJsonTest : MonoBehaviour {
public string aName;
public int aId = 0;
public string aPlate1, aPlate2;
public Color aColor1, aColor2;
public int aPrice1, aPrice2;
// Use this for initialization
void Start ()
{
Restaurant1 rester = new Restaurant1();
rester.MainName = aName;
rester.Character1[0].Name = aName;
string rjson = JsonUtility.ToJson(rester);
File.WriteAllText(Application.dataPath + "/player.json", rjson.ToString());
MyClass myObject = new MyClass();
myObject.level = 1;
myObject.playerName = "Dr Charles Francis";
string json = JsonUtility.ToJson(myObject);
File.WriteAllText(Application.dataPath + "/test.json", json.ToString());
}
// Update is called once per frame
void Update () {
}
}
[Serializable]
public class MyClass
{
public int level;
public string playerName;
}
[Serializable]
public class Restaurant1
{
public string MainName;
public Character1[] Character1;
}
[Serializable]
public class Character1
{
public string Name;
public int Id;
public Plate1[] Plate1;
}
[Serializable]
public class Plate1
{
public string Title;
public Color Color;
public int Price;
}
如果你看一下 MyClass myObject = new MyClass();以及之后的两行。它工作得很好。这是 Unity 文档中的示例。
但是当我尝试使用它并使其变得更加复杂时,我似乎错过了一些东西。
在行
Restaurant1 rester = new Restaurant1();
rester.MainName = aName; //This works.
rester.Character1[0].Name = aName;
//这不起作用。在统一中我得到一个“NullReferenceException:对象引用未设置到对象的实例” 我完全不知道为什么。我在这里做错了什么?
我的问题的第二部分是编写这个东西的最佳方法是什么,以便我可以将“Plates”对象的数量设置为用户定义的数量?然后我假设我需要一个 for 循环来填充每个板对象?
我已经广泛搜索了这方面的示例,但所有示例都是像 myClass 一样完成的。非常简单,没有解释如何构建更复杂的东西。
如果你们能帮忙那就太好了:)
提前致谢。
最佳答案
你们离得很近。您只缺少两个步骤。 rester.Character1
是一个类,同时也是一个数组。
要使其正常工作,您必须声明数组的大小。我们在此示例中使用 4:
rester.Character1 = new Character1[4];
然后,您必须通过创建每个 Character1
类的新实例来初始化元素中的每个类:
for (int i = 0; i < rester.Character1.Length; i++)
{
rester.Character1[i] = new Character1();
}
完成!现在,您可以毫无问题地调用 rester.Character1[0].Name = aName;
。数组是有技巧的,但您必须了解这些步骤。
The second part of my question is what is the best way to write this thing so that i can set the number of "Plates" objects to a user defined amount? and then i would assume i need a for loop to fill in each of the plate objects?
访问 Plates 很棘手,因为这是另一个数组中的一个数组。您需要多个 for 循环才能轻松做到这一点。您可以使用 UI/InputField
从用户处获取输入,然后使用它来创建多个板 block 。
public InputField plateAmount;
//声明每个板数组的大小。从 InputField
/Player 获取车牌数量,然后使用 Convert.ToInt32
将其转换为 int。
for (int i = 0; i < rester.Character1.Length; i++)
{
rester.Character1[i].Plate1 = new Plate1[Convert.ToInt32(plateAmount.text)];
}
现在像我们为Character1类所做的那样创建每个板的实例,但是这个需要多个for循环,由于数组位于另一个数组内部,因此也称为嵌套循环
。
//Create new Instance of Plate1 classes
for (int i = 0; i < rester.Character1.Length; i++)
{
for (int j = 0; j < rester.Character1[i].Plate1.Length; j++)
{
rester.Character1[i].Plate1[j] = new Plate1();
}
}
关于c# - Unity5.3 Json如何使用unities new Json实用程序构造复杂的Json对象,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37477998/