我刚开始用C#涉足,而我一直在敲打我的头以上的JSON反序列化,现在一会儿。我使用Newtonsoft.Json库。我期待字典的数组,这样
I just started dabbling with C#, and I've been banging my head over JSON deserialization for a while now. I'm using Newtonsoft.Json library. I'm expecting just a json response of an array of dictionaries as such
[{"id":"669","content":" testing","comments":"","ups":"0","downs":"0"}, {"id":"482","content":" test2","comments":"","ups":"0","downs":"0"}]
现在我有:(注:下载只是一个字符串持有JSON字符串)
Right now I have: (note: download is just a string holding the json string)
string[] arr = JsonConvert.DeserializeObject<string[]>(download);
我已经尝试了许多不同的这样做的方式,每个失败。有没有一个标准的方式解析这种类型的JSON?
I've tried many different ways of doing this, each failed. Is there a standard way for parsing json of this type?
您有对象不是字符串数组。创建一个映射属性的类和反序列化到了,
You have an array of objects not strings. Create a class that maps the properties and deserialize into that,
public class MyClass {
public string id { get; set; }
public string content { get; set; }
public string ups { get; set; }
public string downs { get; set; }
}
MyClass[] result = JsonConvert.DeserializeObject<MyClass[]>(download);
有只有少数基本类型的JSON,但它是有帮助的学习和识别它们。对象,数组,字符串等 http://www.json.org/ 和的 http://www.w3schools.com/json/default.asp 都是很好的资源开始。例如,一个字符串数组的JSON会是什么样子,
There are only a few basic types in JSON, but it is helpful to learn and recognize them. Objects, Arrays, strings, etc. http://www.json.org/ and http://www.w3schools.com/json/default.asp are good resources to get started. For example a string array in JSON would look like,
["One", "Two", "Three"]