使用JSON反序列化JSON.. NET动态数据



我正在尝试将一些JSON数据反序列化为应用程序的对象。到目前为止,它还可以,因为JSON数据上的属性是静态的(带值的键)。现在我得到了一个结果,其中键是一个动态数据块。

下面是一个JSON url示例:

http://en.wikipedia.org/w/api.php?action=query&格式= json& pageids = 6695,支撑= info

结果JSON为:

{ "query" : { "pages" : { "6695" : { "counter" : "",
          "lastrevid" : 468683764,
          "length" : 8899,
          "ns" : 0,
          "pageid" : 6695,
          "title" : "Citadel",
          "touched" : "2012-01-03T19:16:16Z"
        } } } }

好的,这很好,除了我不能反序列化"pages"数据到一个对象。如果我要为这些页面定义一个类,它应该是这样的:

public class 6695
{
    public string counter { get; set; }
    public int lastrevid { get; set; }
    public int length { get; set; }
    public int ns { get; set; }
    public int pageid { get; set; }
    public string title { get; set; }
    public string touched { get; set; }
}

为了对内容进行反序列化(使用JsonConvert.Deserialize(jsondata))我们都知道我们不能有一个名为6695的类。不仅如此,类的名称也必须不同(例如pageid=7145必须是7145类)。

我似乎可以拔一些值,如果我使用类似JObject.Parse(内容),然后访问项目作为jarray,但它是相当丑陋的,我仍然坚持试图从页面数组中取出数据。

找人帮忙。我不认为这是不寻常的,它只是不是JSON数据我以前遇到过,不知道如何处理它。

谢谢!

PS忘了说这是在Windows Phone 7上,所以"动态"是不可用的!

最简单的方法。在这种特殊情况下,可能是dynamic

dynamic data = Newtonsoft.Json.JsonConvert.DeserializeObject<dynamic>(json);
var lastRevId = data.query.pages["6695"].lastrevid;

你可以通过它的[]名称引用任何元素,所以你可以做一些像data["query"]["pages"]["6695"]["lastrevid"]。这将绕过所有在c#中名字无效的小对象

如何使用https://github.com/facebook-csharp-sdk/simple-json (https://nuget.org/packages/SimpleJson)

var text = "{"query":{"pages":{"6695":{"pageid":6695,"ns":0,"title":"Citadel","touched":"2012-01-03T19:16:16Z","lastrevid":468683764,"counter":"","length":8899}}}}";

(使用动态)
dynamic json = SimpleJson.DeserializeObject(text);
string title = json.query.pages["6695"].title;
foreach (KeyValuePair<string, dynamic> page in json.query.pages)
{
    var id = page.Key;
    var pageId = page.Value.pageid;
    var ns = page.Value.ns;
}

(使用强类型类)

class result
{
    public query query { get; set; }
}
class query
{
    public IDictionary<string, page> pages { get; set; }
}
class page
{
    public long pageid { get; set; }
    public string title { get; set; }
}
var result = SimpleJson.DeserializeObject<result>(text);
(更新)

在不支持动态的Windows phone上,并且你不想使用强类型的类。

var json = (IDictionary<string, object>)SimpleJson.DeserializeObject(text);
var query = (IDictionary<string, object>)json["query"];
var pages = (IDictionary<string, object>)query["pages"];
var pageKeys = pages.Keys;
var page = (IDictionary<string, object>)pages["6695"];
var title = (string)page["title"];

我希望下面的例子会有所帮助。我总是设计一个与json匹配的模型。当对象是您自己的模型设计时,使用它要好得多。

从json中生成c#模型是非常容易的。我使用这个网站来生成模型:http://json2csharp.com

一个完整的例子是:

c#代码:

    var targetsObject = Newtonsoft.Json.JsonConvert.DeserializeObject<YourModel>(jsonString);
JSON:

    {
      "investors": [
        {
          "name": "06",
          "programs": [
            {
              "name": "Conventional",
              "value": "3.5"
            },
            {
              "name": "FHA - Standard",
              "value": "5.0"
            },
            {
              "name": "FHA - Streamline",
              "value": ""
            },
            {
              "name": "VA",
              "value": "5.5"
            },
            {
              "name": "VA IRRRL",
              "value": "6.0"
            },
            {
              "name": "Non-Prime",
              "value": ""
            }
          ]
        },
        {
          "name": "07",
          "programs": [
            {
              "name": "Conventional",
              "value": "3.5"
            },
            {
              "name": "FHA - Standard",
              "value": "5.0"
            },
            {
              "name": "FHA - Streamline",
              "value": "7.0"
            },
            {
              "name": "VA",
              "value": "5.5"
            },
            {
              "name": "VA IRRRL",
              "value": ""
            },
            {
              "name": "Non-Prime",
              "value": ""
            }
          ]
        },
        {
          "name": "08",
          "programs": [
            {
              "name": "Conventional",
              "value": "3.5"
            },
            {
              "name": "FHA - Standard",
              "value": "5.0"
            },
            {
              "name": "FHA - Streamline",
              "value": "7.0"
            },
            {
              "name": "VA",
              "value": "5.5"
            },
            {
              "name": "VA IRRRL",
              "value": ""
            },
            {
              "name": "Non-Prime",
              "value": ""
            }
          ]
        },
        {
          "name": "09",
          "programs": [
            {
              "name": "Conventional",
              "value": "3.5"
            },
            {
              "name": "FHA - Standard",
              "value": "5.0"
            },
            {
              "name": "FHA - Streamline",
              "value": ""
            },
            {
              "name": "VA",
              "value": "5.5"
            },
            {
              "name": "VA IRRRL",
              "value": ""
            },
            {
              "name": "Non-Prime",
              "value": ""
            }
          ]
        },
        {
          "name": "10",
          "programs": [
            {
              "name": "Conventional",
              "value": ""
            },
            {
              "name": "FHA - Standard",
              "value": ""
            },
            {
              "name": "FHA - Streamline",
              "value": ""
            },
            {
              "name": "VA",
              "value": ""
            },
            {
              "name": "VA IRRRL",
              "value": ""
            },
            {
              "name": "Non-Prime",
              "value": "2.0"
            }
          ]
        },
        {
          "name": "11",
          "programs": [
            {
              "name": "Conventional",
              "value": "3.5"
            },
            {
              "name": "FHA - Standard",
              "value": "5.0"
            },
            {
              "name": "FHA - Streamline",
              "value": ""
            },
            {
              "name": "VA",
              "value": "6.0"
            },
            {
              "name": "VA IRRRL",
              "value": "6.0"
            },
            {
              "name": "Non-Prime",
              "value": ""
            }
          ]
        },
        {
          "name": "12",
          "programs": [
            {
              "name": "Conventional",
              "value": "3.5"
            },
            {
              "name": "FHA - Standard",
              "value": "5.0"
            },
            {
              "name": "FHA - Streamline",
              "value": ""
            },
            {
              "name": "VA",
              "value": "5.5"
            },
            {
              "name": "VA IRRRL",
              "value": "6.0"
            },
            {
              "name": "Non-Prime",
              "value": ""
            }
          ]
        },
        {
          "name": "13",
          "programs": [
            {
              "name": "Conventional",
              "value": ""
            },
            {
              "name": "FHA - Standard",
              "value": "5.0"
            },
            {
              "name": "FHA - Streamline",
              "value": ""
            },
            {
              "name": "VA",
              "value": ""
            },
            {
              "name": "VA IRRRL",
              "value": ""
            },
            {
              "name": "Non-Prime",
              "value": "2.0"
            }
          ]
        }
      ]
    }

模型:

    public class Program
    {
        public string name { get; set; }
        public string value { get; set; }
    }
    public class Investor
    {
        public string name { get; set; }
        public List<Program> programs { get; set; }
    }
    public class RootObject
    {
        public List<Investor> investors { get; set; }
    }

使用Json.net可以这样做:

Dictionary<string,object> result = JsonConvert.DeserializeObject<Dictionary<string,object>>(json);
foreach(var item in result)
    Console.WriteLine(item.Key + " " + item.Value);

如何在JSON字符串中进行简单的搜索和替换?虽然它可能不是最优雅的解决方案,但它可能是最实用的解决方案。

也许您可以只使用一个保留属性来包含对象类型,然后使用本文所示的基类型:带JSON的动态类型。净

相关内容

  • 没有找到相关文章

最新更新