Unity5.3 MiniJson Derserialize Multi Tier Json

时间:2016-06-02 14:13:17

标签: c# json list dictionary unity3d

我正在尝试将我的Json文件反序列化为其各种组件。

我创建的Json文件看起来像这样

{  
  "Restaurants":[  
    {  
        "Name":"test",
        "Id":0,
        "PlateSet":[  
            {  
                "Title":"Plate1",
                "Color":{  
                    "r":229,
                    "g":0,
                    "b":20,
                    "a":255
                },
                "Price":12
            }
        ]
    }
  ]
}

我设法获得了姓名,身份证,头衔和价格。但由于某些原因,我似乎无法得到Color。我怀疑它是因为无论我做什么都是为阵列而且Color不是数组。

这是我的代码。

public void DictionaryAdd()
{
    var dict1 = MiniJSON.Json.Deserialize(rJsonData.ToString()) as Dictionary<string, object>;

    var list1 = dict1["Restaurants"] as List<object>;
    Dictionary<string, object> dict2 = list1[0] as Dictionary<string, object>;
    var list2 = dict2["PlateSet"] as List<object>;
    Dictionary<string, object> dict3 = list2[0] as Dictionary<string, object>;
    var list3 = dict3["Color"] as List<object>;

    //var list3 = dict3["Color"] as List<object>;
    //Dictionary<string, int> dict4 = list3[0] as Dictionary<string, int>;

    Debug.Log(dict2["Name"]);
    Debug.Log(dict3["Title"]);
    Debug.Log(dict3["Price"]);
    Debug.Log(dict3["Color"]);
    Debug.Log("D1 " + dict1.Count);
    Debug.Log("L1 " + list1.Count);
    Debug.Log("D2 " + dict2.Count);
    Debug.Log("L2 " + list2.Count);
    Debug.Log("D3 " + dict3.Count);
    Debug.Log("L3 " + list3.Count);

    //Debug.Log(list3.Count);
}

关于我应该怎么做的任何帮助? 也许如果我能在这一点上将颜色转换回颜色,那么如果你知道如何会超级?

对C#来说也很新鲜,所以如果你有一个关于如何做得更好的建议?我全都耳朵:)

非常感谢。

1 个答案:

答案 0 :(得分:0)

我发现您有多个Json个问题。这是一个秘密。

使用自己的JSON轻松

1.复制您的Json文件并将其粘贴here。点击退出按钮。

2.创建测试string并将转换后的信息粘贴到那里(IDE / Mono / VS)。 随着Json的提问,我得到了:

string test = "{  \r\n  \"Restaurants\":[  \r\n    {  \r\n        \"Name\":\"test\",\r\n        \"Id\":0,\r\n        \"PlateSet\":[  \r\n            {  \r\n                \"Title\":\"Plate1\",\r\n                \"Color\":{  \r\n                    \"r\":229,\r\n                    \"g\":0,\r\n                    \"b\":20,\r\n                    \"a\":255\r\n                },\r\n                \"Price\":12\r\n            }\r\n        ]\r\n    }\r\n  ]\r\n}";

3.从您的问题中再次复制原始Json文件。转到here并过去。点击生成按钮。

4.复制该网站的输出类并将它们放入IDE中。现在从每个类中的每个变量中删除所有{ get; set; },并在每个变量后面加上分号。你必须删除它们。

5.将[Serializable]放在您从该网站收到的每个课程的顶部。

6.Done。现在你可以使用

 BaseRestaurant restaurant = new BaseRestaurant();
 restaurant = JsonUtility.FromJson<BaseRestaurant>(test);

将您的字符串转换为类。这对任何遇到Json问题的人都很有用。由于您的Json是另一个类数组中的类数组,因此您需要嵌套for循环以轻松获取Json中的所有信息。使用 Unity 5.4.0.13B 进行测试。它生成的任何类RootObject都是您应该在主代码中引用的唯一类。我不得不将课程重命名为BaseRestaurant以使其更有意义。

以下是最终结果:

[Serializable]
public class Color
{
    public int r;
    public int g;
    public int b;
    public int a;
}

[Serializable]
public class PlateSet
{
    public string Title;
    public Color Color;
    public int Price;
}

[Serializable]
public class Restaurant
{
    public string Name;
    public int Id;
    public List<PlateSet> PlateSet;
}

[Serializable]
public class BaseRestaurant
{
    public List<Restaurant> Restaurants;
}

<强>用法

void Start()
{
    BaseRestaurant restaurant = new BaseRestaurant();
    string test = "{  \r\n  \"Restaurants\":[  \r\n    {  \r\n        \"Name\":\"test\",\r\n        \"Id\":0,\r\n        \"PlateSet\":[  \r\n            {  \r\n                \"Title\":\"Plate1\",\r\n                \"Color\":{  \r\n                    \"r\":229,\r\n                    \"g\":0,\r\n                    \"b\":20,\r\n                    \"a\":255\r\n                },\r\n                \"Price\":12\r\n            }\r\n        ]\r\n    }\r\n  ]\r\n}";

restaurant = JsonUtility.FromJson<BaseRestaurant>(test);
if (restaurant == null)
{
    Debug.Log("Null");
    return;
}

for (int i = 0; i < restaurant.Restaurants.Count; i++)
{
    Debug.Log("Name: " + restaurant.Restaurants[i].Name);
    Debug.Log("ID: " + restaurant.Restaurants[i].Id);
    Debug.Log("PlateSet: " + restaurant.Restaurants[i].PlateSet);

    for (int j = 0; j < restaurant.Restaurants[i].PlateSet.Count; j++)
    {
        Debug.Log("Title: " + restaurant.Restaurants[i].PlateSet[j].Title);

        Debug.Log("Color r: " + restaurant.Restaurants[i].PlateSet[j].Color.r);
        Debug.Log("Color g: " + restaurant.Restaurants[i].PlateSet[j].Color.g);
        Debug.Log("Color b: " + restaurant.Restaurants[i].PlateSet[j].Color.b);
        Debug.Log("Color a: " + restaurant.Restaurants[i].PlateSet[j].Color.a);

        Debug.Log("Color r: " + restaurant.Restaurants[i].PlateSet[j].Price);
    }
  }
}

这需要大约3分钟的时间。