繁体   English   中英

使用json.net将json转换为c#反序列化

[英]json to c# deserialization with json.net

我知道有关json反序列化的无数帖子,但我已经关注了大部分帖子的答案,但他们还没有解决。 我认为我的主要问题是我似乎无法理解Json实际上是如何构建结构的。

我从API中获取了以下Json字符串:

{
  "totalCount_str": "3",
  "items": [
    {
      "standing": 10,
      "corporation": {
        "name": "borkedLabs",
        "isNPC": false,
        "href": "https://crest-tq.eveonline.com/corporations/98046548/",
        "id_str": "98046548",
        "logo": {
          "32x32": {
            "href": "http://imageserver.eveonline.com/Corporation/98046548_32.png"
          },
          "64x64": {
            "href": "http://imageserver.eveonline.com/Corporation/98046548_64.png"
          },
          "128x128": {
            "href": "http://imageserver.eveonline.com/Corporation/98046548_128.png"
          },
          "256x256": {
            "href": "http://imageserver.eveonline.com/Corporation/98046548_256.png"
          }
        },
        "id": 98046548
      },
      "href": "https://crest-tq.eveonline.com/characters/94512721/contacts/98046548/",
      "contact": {
        "id_str": "98046548",
        "href": "https://crest-tq.eveonline.com/corporations/98046548/",
        "name": "borkedLabs",
        "id": 98046548
      },
      "contactType": "Corporation"
    },
    {
      "standing": 10,
      "character": {
        "name": "xxxx yyyy",
        "corporation": {
          "name": "xxyshs",
          "isNPC": false,
          "href": "https://crest-tq.eveonline.com/corporations/98401169/",
          "id_str": "98401169",
          "logo": {
            "32x32": {
              "href": "http://imageserver.eveonline.com/Corporation/98401169_32.png"
            },
            "64x64": {
              "href": "http://imageserver.eveonline.com/Corporation/98401169_64.png"
            },
            "128x128": {
              "href": "http://imageserver.eveonline.com/Corporation/98401169_128.png"
            },
            "256x256": {
              "href": "http://imageserver.eveonline.com/Corporation/98401169_256.png"
            }
          },
          "id": 98401169
        },
        "isNPC": false,
        "href": "https://crest-tq.eveonline.com/characters/95161569/",
        "capsuleer": {
          "href": "https://crest-tq.eveonline.com/characters/95161569/capsuleer/"
        },
        "portrait": {
          "32x32": {
            "href": "http://imageserver.eveonline.com/Character/95161569_32.jpg"
          },
          "64x64": {
            "href": "http://imageserver.eveonline.com/Character/95161569_64.jpg"
          },
          "128x128": {
            "href": "http://imageserver.eveonline.com/Character/95161569_128.jpg"
          },
          "256x256": {
            "href": "http://imageserver.eveonline.com/Character/95161569_256.jpg"
          }
        },
        "id": 95161569,
        "id_str": "95161569"
      },
      "contact": {
        "id_str": "95161569",
        "href": "https://crest-tq.eveonline.com/characters/95161569/",
        "name": "xxxx yyyy",
        "id": 95161569
      },
      "href": "https://crest-tq.eveonline.com/characters/94512769/contacts/95161569/",
      "contactType": "Character",
      "watched": false,
      "blocked": false
    },
    {
      "standing": -10,
      "alliance": {
        "id_str": "99000003",
        "href": "http://crest.regner.dev/alliances/99000003/",
        "id": 99000003,
        "name": "One One Corporation Alliance"
      },
      "href": "http://crest.regner.dev/characters/90000001/contacts/99000003/",
      "contact": {
        "id_str": "99000003",
        "href": "http://crest.regner.dev/alliances/99000003/",
        "name": "One One Corporation Alliance",
        "id": 99000003
      },
      "contactType": "Alliance"
    }
  ],
  "pageCount": 1,
  "pageCount_str": "1",
  "totalCount": 3
}

请注意,items数组可以包含任意数量的“联系人”。

通过使用http://json2csharp.com/我已将其转换为C#中的类,如下所示:

public class Contacts
{
    public string totalCount_str { get; set; }
    public Item[] items { get; set; }
    public int pageCount { get; set; }
    public string pageCount_str { get; set; }
    public int totalCount { get; set; }
}

public class Item
{
    public int standing { get; set; }
    public Alliance alliance { get; set; }
    public string href { get; set; }
    public Contact contact { get; set; }
    public string contactType { get; set; }
    public Character character { get; set; }
    public bool watched { get; set; }
    public bool blocked { get; set; }
}

public class Alliance
{
    public string id_str { get; set; }
    public string href { get; set; }
    public int id { get; set; }
    public string name { get; set; }
}

public class Contact
{
    public string id_str { get; set; }
    public string href { get; set; }
    public string name { get; set; }
    public int id { get; set; }
}

public class Character
{
    public string name { get; set; }
    public Corporation corporation { get; set; }
    public bool isNPC { get; set; }
    public string href { get; set; }
    public Capsuleer capsuleer { get; set; }
    public Portrait portrait { get; set; }
    public int id { get; set; }
    public string id_str { get; set; }
}

public class Corporation
{
    public string name { get; set; }
    public bool isNPC { get; set; }
    public string href { get; set; }
    public string id_str { get; set; }
    public Logo logo { get; set; }
    public int id { get; set; }
}

public class Logo
{
    public _32X32 _32x32 { get; set; }
    public _64X64 _64x64 { get; set; }
    public _128X128 _128x128 { get; set; }
    public _256X256 _256x256 { get; set; }
}

public class _32X32
{
    public string href { get; set; }
}

public class _64X64
{
    public string href { get; set; }
}

public class _128X128
{
    public string href { get; set; }
}

public class _256X256
{
    public string href { get; set; }
}

public class Capsuleer
{
    public string href { get; set; }
}

public class Portrait
{
    public _32X32 _32x32 { get; set; }
    public _64X64 _64x64 { get; set; }
    public _128X128 _128x128 { get; set; }
    public _256X256 _256x256 { get; set; }
}

然后尝试使用以下命令对其进行反序列化:

List<Contacts> tempList = Newtonsoft.Json.JsonConvert.DeserializeObject<List<Contacts>>(response.Content);

我感谢任何可以让我走上正确轨道的帮助。 我在尝试这种方式时遇到错误:

Cannot deserialize the current JSON object (e.g. {"name":"value"}) into type 'System.Collections.Generic.List`1[ContactWatchlister.Models.Contacts]' because the type requires a JSON array (e.g. [1,2,3]) to deserialize correctly.
To fix this error either change the JSON to a JSON array (e.g. [1,2,3]) or change the deserialized type so that it is a normal .NET type (e.g. not a primitive type like integer, not a collection type like an array or List<T>) that can be deserialized from a JSON object. JsonObjectAttribute can also be added to the type to force it to deserialize from a JSON object.
Path 'totalCount_str', line 1, position 18.

我目前陷入困境,因为我无法理解它为什么无法将“totalCount_str”理解为字符串。

我试图在我的类中的所有属性上使用[JsonProperty("attribute")]来模拟它是正确的,但同样的错误。

我很确定我做的事情相当简单,错误,但我无法绕过它。 希望你能帮忙! :-)

如果response.Content包含您描述的JSON字符串,我认为该语句应为:

var tempList = Newtonsoft.Json.JsonConvert.DeserializeObject<Contacts>(response.Content);

您的JSON在最外层“级别”中具有"totalCount_str"itemspageCountpageCount_strtotalCount字段,因此它只能被反序列化为具有这些属性的类(或具有与这些字段名对应的正确JsonAttribute的属性)。

您尝试过的那个将使用以下JSON,其中最外层的实体是一个数组:

[   {
    "totalCount_str": "3",
    "items": [
      {
        "standing": 10,
        "corporation": {
          "name": "borkedLabs",
          "isNPC": false,
          "href": "https://crest-tq.eveonline.com/corporations/98046548/",
          "id_str": "98046548",
          "logo": {
            "32x32": {
              "href": "http://imageserver.eveonline.com/Corporation/98046548_32.png"
            },
            "64x64": {
              "href": "http://imageserver.eveonline.com/Corporation/98046548_64.png"
            },
            "128x128": {
              "href": "http://imageserver.eveonline.com/Corporation/98046548_128.png"
            },
            "256x256": {
              "href": "http://imageserver.eveonline.com/Corporation/98046548_256.png"
            }
          },
          "id": 98046548
        },
        "href": "https://crest-tq.eveonline.com/characters/94512721/contacts/98046548/",
        "contact": {
          "id_str": "98046548",
          "href": "https://crest-tq.eveonline.com/corporations/98046548/",
          "name": "borkedLabs",
          "id": 98046548
        },
        "contactType": "Corporation"
      },
      {
        "standing": 10,
        "character": {
          "name": "xxxx yyyy",
          "corporation": {
            "name": "xxyshs",
            "isNPC": false,
            "href": "https://crest-tq.eveonline.com/corporations/98401169/",
            "id_str": "98401169",
            "logo": {
              "32x32": {
                "href": "http://imageserver.eveonline.com/Corporation/98401169_32.png"
              },
              "64x64": {
                "href": "http://imageserver.eveonline.com/Corporation/98401169_64.png"
              },
              "128x128": {
                "href": "http://imageserver.eveonline.com/Corporation/98401169_128.png"
              },
              "256x256": {
                "href": "http://imageserver.eveonline.com/Corporation/98401169_256.png"
              }
            },
            "id": 98401169
          },
          "isNPC": false,
          "href": "https://crest-tq.eveonline.com/characters/95161569/",
          "capsuleer": {
            "href": "https://crest-tq.eveonline.com/characters/95161569/capsuleer/"
          },
          "portrait": {
            "32x32": {
              "href": "http://imageserver.eveonline.com/Character/95161569_32.jpg"
            },
            "64x64": {
              "href": "http://imageserver.eveonline.com/Character/95161569_64.jpg"
            },
            "128x128": {
              "href": "http://imageserver.eveonline.com/Character/95161569_128.jpg"
            },
            "256x256": {
              "href": "http://imageserver.eveonline.com/Character/95161569_256.jpg"
            }
          },
          "id": 95161569,
          "id_str": "95161569"
        },
        "contact": {
          "id_str": "95161569",
          "href": "https://crest-tq.eveonline.com/characters/95161569/",
          "name": "xxxx yyyy",
          "id": 95161569
        },
        "href": "https://crest-tq.eveonline.com/characters/94512769/contacts/95161569/",
        "contactType": "Character",
        "watched": false,
        "blocked": false
      },
      {
        "standing": -10,
        "alliance": {
          "id_str": "99000003",
          "href": "http://crest.regner.dev/alliances/99000003/",
          "id": 99000003,
          "name": "One One Corporation Alliance"
        },
        "href": "http://crest.regner.dev/characters/90000001/contacts/99000003/",
        "contact": {
          "id_str": "99000003",
          "href": "http://crest.regner.dev/alliances/99000003/",
          "name": "One One Corporation Alliance",
          "id": 99000003
        },
        "contactType": "Alliance"
      }
    ],
    "pageCount": 1,
    "pageCount_str": "1",
    "totalCount": 3   } ]

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM