当前位置:  开发笔记 > 编程语言 > 正文

转换部分json到c#对象时发生了'Newtonsoft.Json.JsonReaderException'类型的第一次机会异常

如何解决《转换部分json到c#对象时发生了'Newtonsoft.Json.JsonReaderException'类型的第一次机会异常》经验,为你挑选了0个好方法。

所以,在这里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);
IList results = 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"
}

不知道为什么会发生这种异常,想知道如何修复它.

推荐阅读
低调pasta_730
这个屌丝很懒,什么也没留下!
DevBox开发工具箱 | 专业的在线开发工具网站    京公网安备 11010802040832号  |  京ICP备19059560号-6
Copyright © 1998 - 2020 DevBox.CN. All Rights Reserved devBox.cn 开发工具箱 版权所有