首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Unity5.3 Json如何使用unities新Json实用程序构造复杂的Json对象

Unity5.3 Json如何使用unities新Json实用程序构造复杂的Json对象
EN

Stack Overflow用户
提问于 2016-05-27 07:59:46
回答 1查看 343关注 0票数 2

我有两个部分的问题。首先,我试图编写这样的代码,它可以编写类似于这样的json文件。

代码语言:javascript
复制
{ 
"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
         }
       ]
     }
  ]
}

到目前为止我的代码是这样的。

代码语言:javascript
复制
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 =新的MyClass();以及后面的2行。它工作得很完美。这就是来自Unity的例子。

但当我试图使用它并使它变得更加复杂时,我似乎遗漏了一些东西。

排在队伍里

代码语言:javascript
复制
Restaurant1 rester = new Restaurant1();

rester.MainName = aName; //This works.

rester.Character1[0].Name = aName; 

//这不起作用。统一起来,我得到了一个"NullReferenceException: Object,没有设置为一个对象的实例“,我完全不知道为什么。我在这里做错什么了?

我问题的第二部分是,写这个东西的最佳方法是什么,这样我就可以将“板块”对象的数量设置为用户定义的数量。然后假设我需要一个for循环来填充每个板块对象?

我已经搜索了很多关于这个的例子,但是所有的例子都像myClass一样。非常简单,没有解释如何构建更复杂的东西。

如果你们能帮忙,那就太棒了:)

提前谢谢。

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2016-05-27 09:00:07

你离我这么近。你只错过了两步。rester.Character1是一个类,同时也是一个数组.

要使其工作,必须声明数组的大小。让我们在这个示例中使用4

代码语言:javascript
复制
rester.Character1 = new Character1[4];

然后,必须通过创建元素的新实例来初始化元素中的每个Character1类:

代码语言:javascript
复制
 for (int i = 0; i < rester.Character1.Length; i++)
 {
     rester.Character1[i] = new Character1();
 }

完成了!现在,您可以没有任何问题地调用rester.Character1[0].Name = aName;。数组是骗人的,但您必须理解这些步骤。

我问题的第二部分是,写这个东西的最佳方法是什么,这样我就可以将“板块”对象的数量设置为用户定义的数量。然后假设我需要一个for循环来填充每个板块对象?

访问板块是很困难的,因为这是另一个数组中的一个数组。您需要多个for循环才能轻松地做到这一点。您可以使用UI/InputField从用户获得输入,然后使用该输入创建车牌数量。

代码语言:javascript
复制
public InputField plateAmount;

//声明每个板块数组的大小。从InputField/Player中获取盘子的数量,然后用Convert.ToInt32将其转换为int。

代码语言:javascript
复制
for (int i = 0; i < rester.Character1.Length; i++)
{
    rester.Character1[i].Plate1 = new Plate1[Convert.ToInt32(plateAmount.text)];
} 

现在,创建每个板块的实例,就像我们对Character1类所做的一样,但是这个例子需要多个for循环,这也称为Nested Loop,因为它位于另一个数组中。

代码语言:javascript
复制
//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();
    }
}
票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/37477998

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档