【问题标题】:JSON to C# classes IssueJSON 到 C# 类问题
【发布时间】:2013-09-16 23:22:24
【问题描述】:

我对使用 JSON 还很陌生,我对从 Web 服务获得的 JSON 响应有些问题。我已经尝试了一堆在线“JSON 到 c# 类”生成器,但它们似乎都不能与这个特定的 JSON 一起工作。我正在使用 Pocket API 以及我在下面得到的响应。谁能指出我将其格式化为课程的正确方向?

    {
    "status": 1,
    "list": {
        "229279689": {
            "item_id": "229279689",
            "resolved_id": "229279689",
            "given_url": "http://www.grantland.com/blog/the-triangle/post/_/id/38347/ryder-cup-   preview",
            "given_title": "The Massive Ryder Cup Preview - The Triangle Blog - Grantland",
            "favorite": "0",
            "status": "0",
            "resolved_title": "The Massive Ryder Cup Preview",
            "resolved_url": "http://www.grantland.com/blog/the-triangle/post/_/id/38347/ryder-cup-preview",
            "excerpt": "The list of things I love about the Ryder Cup is so long that it could fill a (tedious) novel, and golf fans can probably guess most of them.",
            "is_article": "1",
            "has_video": "1",
            "has_image": "1",
            "word_count": "3197",
            "images": {
                "1": {
                    "item_id": "229279689",
                    "image_id": "1",
                    "src": "http://a.espncdn.com/combiner/i?img=/photo/2012/0927/grant_g_ryder_cr_640.jpg&w=640&h=360",
                    "width": "0",
                    "height": "0",
                    "credit": "Jamie Squire/Getty Images",
                    "caption": ""
                }
            },
            "videos": {
                "1": {
                    "item_id": "229279689",
                    "video_id": "1",
                    "src": "http://www.youtube.com/v/Er34PbFkVGk?version=3&hl=en_US&rel=0",
                    "width": "420",
                    "height": "315",
                    "type": "1",
                    "vid": "Er34PbFkVGk"
                }
            }
        }
    }
}

感谢大家的指点,

【问题讨论】:

  • 我把它复制到json.parser.online.fr 并且解析正常。
  • 您需要完全代表 JSON 的 C# 对象吗?我假设您需要解析您感兴趣的值,然后将其存储在您自己的 C# 对象中。

标签: c# json pocket


【解决方案1】:

有点棘手,但这应该可以。 (因为像229279689这样的属性名称)

var result = JsonConvert.DeserializeObject<Root>(json);

public class MyImage
{
    public string item_id { get; set; }
    public string image_id { get; set; }
    public string src { get; set; }
    public string width { get; set; }
    public string height { get; set; }
    public string credit { get; set; }
    public string caption { get; set; }
}

public class MyVideo
{
    public string item_id { get; set; }
    public string video_id { get; set; }
    public string src { get; set; }
    public string width { get; set; }
    public string height { get; set; }
    public string type { get; set; }
    public string vid { get; set; }
}

public class MyListItem
{
    public string item_id { get; set; }
    public string resolved_id { get; set; }
    public string given_url { get; set; }
    public string given_title { get; set; }
    public string favorite { get; set; }
    public string status { get; set; }
    public string resolved_title { get; set; }
    public string resolved_url { get; set; }
    public string excerpt { get; set; }
    public string is_article { get; set; }
    public string has_video { get; set; }
    public string has_image { get; set; }
    public string word_count { get; set; }
    public Dictionary<string, MyImage> images; // <---
    public Dictionary<string, MyVideo> videos; // <---
}

public class Root
{
    public int status;
    public Dictionary<string, MyListItem> list; // <---
}

【讨论】:

    【解决方案2】:

    你试过Json.NET:

    JObject o = JObject.Parse("some json");
    string name = (string)o["Name"];
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2023-03-17
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-06-13
      相关资源
      最近更新 更多