所以,在这里JsonResult jr
,它看起来像这样(输出是一行,我在这里格式化):
string jr = {"Results": [ {"Code":"DEMO", "Id":"1285", "Office":"9881", "Customers": [ 2713, 94204 ], "Account":196, "Appointments": [ 14, 58 ], "Role":0, "UserName":"demo", "UserId":3669, "FirstName":"Peter", "LastName":"Pan", "Phones": [ "(888) 888-8888" ], "Fax":null, "Email":"test@test.com", "SMS":null, "RecordStatus":"1"}, {"Code":"DEMO", "Id":"9292", "Office":"9881", "Customers": [ 13, 904 ], "Account":196, "Appointments": [ 14, 58 ], "Role":0, "UserName":"berry", "UserId":302, "FirstName":"Jimmy", "LastName":"White", "Phones": [ "(888) 888-8888" ], "Email":"test@test.com", "SMS":null, "RecordStatus":"1"} ], "TotalResults":2, "MilliSeconds":4}
这是我的对象User
:
public class User { public string FirstName { get; set; } public string LastName { get; set; } public string Id { get; set; } public string Office { get; set; } public string Email { get; set; } public string Code { get; set; } }
我正在尝试使用反序列化部分JSON片段将json映射到我的对象:http: //www.newtonsoft.com/json/help/html/SerializingJSONFragments.htm
我按照这个例子,但是我收到了一个错误:在Newtonsoft.Json.dll中出现了'Newtonsoft.Json.JsonReaderException'类型的第一次机会异常.
网上有很多人说这是由坏json造成的.我查了一下,没发现自己有什么问题.以下是我的代码:
JObject response = JObject.Parse(jr); IListresults = response["Results"].Children().ToList(); IList searchResults = new List (); foreach(JToken result in results) { System.Diagnostics.Debug.WriteLine(result); //just to check my json data. User searchResult = JsonConvert.DeserializeObject (results.ToString()); //get exception on this line. searchResults.Add(searchResult); }
第一个result
输出如下所示:
{ "Code":"DEMO", "Id":"1285", "Office":"9881", "Customers": [ 2713, 94204 ], "Account":196, "Appointments": [ 14, 58 ], "Role":0, "UserName":"demo", "UserId":3669, "FirstName":"Peter", "LastName":"Pan", "Phones": [ "(888) 888-8888" ], "Fax":null, "Email":"test@test.com", "SMS":null, "RecordStatus":"1" }
不知道为什么会发生这种异常,想知道如何修复它.