使用带有动态数据的 JSon.NET 反序列化 JSON [英] Deserializing JSON using JSon.NET with dynamic data

查看:24
本文介绍了使用带有动态数据的 JSon.NET 反序列化 JSON的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试将一些 JSON 数据反序列化为应用程序的对象.到目前为止,这一切都很好,因为 JSON 数据上的属性是静态的(带有值的键).现在我得到了一个结果,其中键是一段动态数据.

I'm trying to deserialize some JSON data into objects for an application. Up until now it's been fine because the properties on the JSON data was static (key with a value). Now I've got a result where the key is a dynamic piece of data.

这是一个示例 JSON 网址:

Here's an example JSON url:

http://en.wikipedia.org/w/api.php?action=query&format=json&pageids=6695&prop=info

由此产生的 JSON 是:

The resulting JSON for this is:

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

好的,这很好,只是我不能将页面"数据反序列化为一个对象.如果我要为页面定义一个类,它必须如下所示:

Okay, that's great except I can't deserialize the "pages" data into an object. If I were to define a class for the pages it would have to look like this:

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 类).

In order to deserialze the contents (using JsonConvert.Deserialize(jsondata)) and we all know we can't have a class called 6695. Not only that, the name of the class would have to be different (for example pageid=7145 would have to be the 7145 class).

如果我使用 JObject.Parse(content) 之类的东西,然后将项目作为 JArray 访问,我似乎可以提取一些值,但它非常难看,而且我仍然试图从 pages 数组中获取数据.

I can seem to pluck some values out if I use something like JObject.Parse(content) and then access items as JArrays but it's pretty ugly and I'm still stuck on trying to get out the data from the pages array.

正在寻找可以帮助解决此问题的人.我认为这并不罕见,只是我之前遇到过的 JSON 数据不知道如何处理.

Looking for someone to help with this. I don't think it's uncommon, it's just not JSON data I've come across before and not sure how to handle it.

谢谢!

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

PS forgot to mention this is on Windows Phone 7 so "dynamic" isn't available!

推荐答案

这里是如何使用 https://github.com/facebook-csharp-sdk/simple-json ( https://nuget.org/packages/SimpleJsona>).

Here is how you do using 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 手机上.

on windows phone where dynamic is not supported and you don't want to use strongly typed classes.

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.NET 反序列化 JSON的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆