获取JSON对象节点 [英] Get JSON object node

查看:182
本文介绍了获取JSON对象节点的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个JSON对象,我想要得到的值:
实体>媒体>尺寸>大型> ^ h

有没有办法得到它像XML - > Xpath的方法

这是额外的线路是无关紧要的质疑只是因为...

  {
  created_at:孙,2012年1月1日17时05分32秒+0000
  实体:{
    user_mentions:[
      {
        SCREEN_NAME:nurdanterbiyik,
        名:nurdan
        ID:264782080,
        ID_STR:264782080,
        指标:
          0,
          15
        ]
      }
    ]
    传媒:
      {
        ID:153522253777219584,
        ID_STR:153522253777219584,
        指标:
          44,
          64
        ]
        MEDIA_URL:http://p.twimg.com/AiFrrSmCMAAdEID.jpg,
        media_url_https:https://p.twimg.com/AiFrrSmCMAAdEID.jpg,
        URL:http://t.co/ZwHN9gvO
        DISPLAY_URL:pic.twitter.com/ZwHN9gvO
        expanded_url:http://twitter.com/emelkiraac/status/153522253773025280/photo/1,
        类型:照片,
        大小:{
          大: {
            W:536,
            H:800,
            调整大小:适合
          },


解决方案

使用 JSON.NET 你有几种方式无需您的JSON文本反序列化到对象中读取数据。下面是一个简单的例子:

  JSON字符串= @{
created_at:太阳,2012年1月1日17时05分32秒+0000
  实体:{
    传媒:[{
      类型:图片,
      大小:{
        大: {
          W:536,
          H:800,
          调整:适合
        }
      }
    }]
  }
}
JObject O = JObject.Parse(JSON);
INT H =(INT)O [实体] [媒体] [0] [尺寸] [大] [H];
INT H2 =(int)的o.SelectToken(entities.media [0] .sizes.large.h);

I have a json object and I want to get the value of : entities > media > sizes > large > h

Is there a way to get it like XML -> Xpath method?

This is extra lines that is irrelevant to question just because of ...

{
  "created_at": "Sun, 01 Jan 2012 17:05:32 +0000",
  "entities": {
    "user_mentions": [
      {
        "screen_name": "nurdanterbiyik",
        "name": "nurdan",
        "id": 264782080,
        "id_str": "264782080",
        "indices": [
          0,
          15
        ]
      }
    ],
    "media": [
      {
        "id": 153522253777219584,
        "id_str": "153522253777219584",
        "indices": [
          44,
          64
        ],
        "media_url": "http://p.twimg.com/AiFrrSmCMAAdEID.jpg",
        "media_url_https": "https://p.twimg.com/AiFrrSmCMAAdEID.jpg",
        "url": "http://t.co/ZwHN9gvO",
        "display_url": "pic.twitter.com/ZwHN9gvO",
        "expanded_url": "http://twitter.com/emelkiraac/status/153522253773025280/photo/1",
        "type": "photo",
        "sizes": {
          "large": {
            "w": 536,
            "h": 800,
            "resize": "fit"
          },

解决方案

Using JSON.NET you have several ways to read data without having to deserialize your JSON text into objects. Here is a simplified example:

string json = @" {
""created_at"": ""Sun, 01 Jan 2012 17:05:32 +0000"",
  ""entities"": {
    ""media"": [{
      ""type"": ""photo"",
      ""sizes"": {
        ""large"": {
          ""w"": 536,
          ""h"": 800,
          ""resize"": ""fit""
        }
      }
    }]
  }
}
";

JObject o = JObject.Parse(json);
int h = (int)o["entities"]["media"][0]["sizes"]["large"]["h"];
int h2 = (int)o.SelectToken("entities.media[0].sizes.large.h");

这篇关于获取JSON对象节点的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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