反序列化使用Newtonsoft JSON.NET动态JSON字符串 [英] Deserialize Dynamic Json string using Newtonsoft JSON.NET

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

问题描述

我有我从Facebook API得到一个JSON字符串,在我所根据其内容,其名称更改的节点,例如一些时间,它是45或58等
这可能是任何数字。
我想它的价值。如何获得的呢?
示例:

I have a JSON string that I'm getting from Facebook API, in which I have a node whose name changes according to its content, for example some time it is 45, or 58 etc. It could be any number. I want its value. How to get it? Example:

{
"data": [
{
  "id": "1492292372_10201810786059989", 
  "created_time": "2014-04-05T09:00:54+0000"
}, 
{
  "id": "1492292372_10201804679827337", 
  "created_time": "2014-04-04T07:29:07+0000"
}, 
{
  "id": "1492292372_10201804649306574", 
  "created_time": "2014-04-04T07:10:33+0000"
}, 
{
  "id": "1492292372_10201801316823264", 
  "created_time": "2014-04-03T18:31:50+0000"
}, 
{
  "id": "1492292372_10201798962284402", 
  "created_time": "2014-04-03T06:24:47+0000"
}, 
{
  "message_tags": {
    "0": [
      {
        "id": "1492292372", 
        "name": "Yawar Sohail", 
        "type": "user", 
        "offset": 0, 
        "length": 12
      }
    ], 
    "15": [
      {
        "id": "1489845168", 
        "name": "Zeeshan Anjum", 
        "type": "user", 
        "offset": 15, 
        "length": 13
      }
    ]
  }, 
  "id": "1492292372_10201796274777216", 
  "created_time": "2014-04-02T17:57:05+0000"
}, 
{
  "id": "1492292372_10201794080482360", 
  "created_time": "2014-04-02T07:26:23+0000"
}, 

内message_tags有两个节点[0和15],他们根据自己的偏移值动态地变化。我想这些节点的内部名称,类型和IDS

Inside message_tags there are two nodes [0 and 15] they dynamically changes according to their offset values. I want names, type and ids inside these nodes.

推荐答案

您可以反序列化的JSON成的ExpandoObject

You can deserialize your JSON into an ExpandoObject:

var converter = new ExpandoObjectConverter();
dynamic obj = JsonConvert.DeserializeObject<ExpandoObject>(json, converter);



可动态添加成员在运行时你的对象,并允许您遍历它们的as在这个答案描述。

这样,你可以遍历 obj.message_tags 来获取个人信息,并分别获得他们的所有细节。

That way you can iterate over obj.message_tags to get the individual messages, and obtain all their details respectively.

这篇关于反序列化使用Newtonsoft JSON.NET动态JSON字符串的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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