Newtonsoft.Json解析数组
以下是遗传json数组:
var jsonInfo=[{"name":"abc","id":"1","count":0},{"name":"def","id":"2","count":1}]
如果使用传统的JsonConvert.DeserializeObject方法会报错,这时需要使用以下方法:
var arrdata = Newtonsoft.Json.Linq.JArray.Parse(jsoninfo);
List<UserInfo> obj2 = arrdata.ToObject<List<UserInfo>>();
string json = @"{ CPU: 'Intel', Drives: [ 'DVD read/writer', '500 gigabyte hard drive' ] }"; JObject o = JObject.Parse(json);
string json = @"[ 'Small', 'Medium', 'Large' ]"; JArray a = JArray.Parse(json);
using (StreamReader reader = File.OpenText(@"c:\person.json")) { JObject o = (JObject)JToken.ReadFrom(new JsonTextReader(reader)); // do stuff }