【问题标题】:Unity5.3 MiniJson Derserialize Multi Tier JsonUnity5.3 MiniJson 反序列化多层 Json
【发布时间】:2016-06-02 14:13:16
【问题描述】:

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

我创建的Json文件是这样的

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

我已经设法获取了名称、ID、标题和价格。但由于某种原因,我似乎无法取出颜色。我怀疑它是因为我所做的一切都是为了数组而颜色不是数组。

这是我的代码。

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# 也很陌生,所以如果你对我如何能做得更好有什么建议?我全神贯注:)

非常感谢。

【问题讨论】:

    标签: c# json list dictionary unity3d


    【解决方案1】:

    我看到您有多个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.完成。现在你可以使用

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

    将您的字符串转换为类。这对于任何对 Json 有问题的人都会很有用。由于您的 Json 是另一个类数组中的类数组,因此您需要嵌套 for 循环以轻松地从 Json 获取所有信息。使用 Unity 5.4.0.13B 进行测试。它生成的任何显示RootObject 的类都是您应该在主代码中引用的only 类。我不得不将课程重命名为 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 分钟。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-04-13
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多