【问题标题】:JSON string serialization (Newtonsoft.JSON)JSON 字符串序列化 (Newtonsoft.JSON)
【发布时间】:2014-11-12 03:38:04
【问题描述】:

我得到一个 JSON 字符串作为 HTTP 响应。这个字符串看起来像:

response: {
    count: 524,
    items: [{
        id: 318936948,
        owner_id: 34,
        artist: 'The Smiths',
        title: 'How Soon Is Now',
        duration: 233,
        url: 'link',
        genre_id: 9
    }, {
        id: 312975563,
        owner_id: 34,
        artist: 'Thom Yorke',
        title: 'Guess Again!',
        duration: 263,
        url: 'link',
        genre_id: 22
    }]
}

我有 Newtonsoft.Json 库,以及类 Response 和 Item:

[JsonObject(MemberSerialization.OptIn)]
class Response
{
    [JsonProperty("count")]
    public int count { get; set; }
    [JsonProperty("items")]
    public List<Item> items { get; set; }
}
[JsonObject(MemberSerialization.OptOut)]
class Item
{
    public string aid { get; set; }
    public string owner_id { get; set; }
    public string artist { get; set; }
    public string title { get; set; }
    public string duration { get; set; }
    public string url { get; set; }
    public int lyrics_id { get; set; }
    public int album_id { get; set; }
    public int genre_id { get; set; }
}

我像这样反序列化它:

Response r = JsonConvert.DeserializeObject<Response>(line);

它不起作用,“r”保持为空。我错在哪里,为什么?正在编译,没有任何异常。

【问题讨论】:

  • 当我将您的 Json 粘贴到 jsonformatter.curiousconcept.com 时出现一堆错误,例如“字符串应该用双引号括起来。[代码 17,结构 2]”。
  • Json 似乎是错误的。在这里查看json2csharp.com
  • 另外,看起来response 是某些包含结构中的字段/属性,但您没有显示出来。

标签: c# json json.net


【解决方案1】:

这里有几个问题:

  1. 您的 JSON 字符串缺少外括号。它应该看起来像

    { response: {
        count: 524,
        items: [{
            id: 318936948,
            owner_id: 34,
            artist: 'The Smiths',
            title: 'How Soon Is Now',
            duration: 233,
            url: 'link',
            genre_id: 9
        }, {
            id: 312975563,
            owner_id: 34,
            artist: 'Thom Yorke',
            title: 'Guess Again!',
            duration: 263,
            url: 'link',
            genre_id: 22
        }]
    }}
    
  2. 您正在尝试反序列化 Response 类,但该类中没有字段 response,它显然是某些包含类中的字段。所以,你需要提取出实际的Response

  3. 您在Item 中的属性aid 需要命名为id

所以,以下似乎可行:

            // Fix missing outer parenthesis
            var fixedLine = "{" + line + "}";

            // Parse into a JObject
            var mapping = JObject.Parse(fixedLine);

            // Extract the "response" and deserialize it.
            Response r = mapping["response"].ToObject<Response>();

            Debug.WriteLine(r.count);
            foreach (var item in r.items)
            {
                Debug.WriteLine("  " + JsonConvert.SerializeObject(item));
            }

这会产生调试输出

524
  {"id":"318936948","owner_id":"34","artist":"The Smiths","title":"How Soon Is Now","duration":"233","url":"link","lyrics_id":0,"album_id":0,"genre_id":9}
  {"id":"312975563","owner_id":"34","artist":"Thom Yorke","title":"Guess Again!","duration":"263","url":"link","lyrics_id":0,"album_id":0,"genre_id":22}

并显示数据反序列化成功。

【讨论】:

  • 它是这样工作的,我没有固定线路,但使用 JObject 可以反序列化。但我还是不明白,有什么问题。
【解决方案2】:

您的代码对我来说按原样工作。您收到的 JSON 字符串是否在开头包含 response: 位?如果是这样,您需要删除它(删除字符串中第一个 { 字符之前的所有内容),那么它应该适合您。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-12-21
    • 1970-01-01
    • 2023-03-23
    • 2021-09-02
    相关资源
    最近更新 更多