我正在尝试将我的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#也很陌生,所以如果你对我如何做得更好有建议的话?我洗耳恭听:)
谢谢阿洛特。
发布于 2016-06-02 15:08:32
我看你有多个Json问题。这里有个秘密。
为您自己的JSON提供了一个简单的。
1.复制Json文件并粘贴到这里。单击逃逸按钮。
2.创建一个测试string并将转换后的信息粘贴到那里(IDE/Mono/VS)。在你的问题中我得到的是:
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文件从你的问题中再拷贝一次。Go 这里和past it.Click,生成按钮。
4.从该网站复制输出类,并将它们放在IDE中。现在,从每个类中的每个变量中删除所有的{ get; set; },并将分号放在每个变量之后。您必须移除它们。
5.把[Serializable]放在你从那个网站收到的每一堂课的顶端。
6.已完成。现在你可以用
BaseRestaurant restaurant = new BaseRestaurant();
restaurant = JsonUtility.FromJson<BaseRestaurant>(test);将字符串转换为类。这对任何与Json有问题的人都是有用的。由于Json是另一个类数组中的类数组,所以需要嵌套for循环来轻松地从Json获取所有信息。用Unity5.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分钟的时间。
https://stackoverflow.com/questions/37594395
复制相似问题