C#with Newtonsoft JSON.Net,如何解析更复杂的JSON对象

时间:2016-11-16 13:38:11

标签: .net json json.net

我开始使用Newtonsoft的JSON.net来解析JSON文件。我可以找到关于如何解析多层深层对象的任何好教程。

你可以帮我举个例子吗?谢谢。

例如,我想获得Search.results的值16513100,以及" Tweets"中的每个节点。部分,content->Poloarity's value = Neutral

到目前为止我所拥有的:

JObject o1 = JObject.Parse(System.IO.File.ReadAllText(filename));

string TotalCount = ((JProperty)((JContainer)((JContainer)o1.First).First).First).Value.ToString();
string CurrentCount = ((JProperty)((JContainer)((JContainer)o1.First).First).Last).Value.ToString();

以下是JSON文本:

{
  "related": { "next": { "href": "https://cdeservice.mybluemix.net:443/api/v1/messages/search?q=Apple&from=100&size=100" } },
  "search": {
    "results": 16513100,
    "current": 100
  },
  "tweets": [
    {
      "cde": {
        "author": {
          "gender": "unknown",
          "parenthood": {
            "isParent": "unknown",
            "evidence": ""
          },
          "location": { },
          "maritalStatus": {
            "isMarried": "unknown",
            "evidence": ""
          }
        },
        "content": {
          "sentiment": {
            "evidence": [ ],
            "polarity": "NEUTRAL"
          }
        }
      },
      "cdeInternal": { "tracks": [ { "id": "2713720a-f341-4ca0-acd1-f8b1e6c0d00f" } ] },
      "message": {
        "postedTime": "2014-10-18T14:52:52.000Z",
        "verb": "share",
        "link": "http://twitter.com/CyMadD0x/statuses/523486878075744257",
        "generator": {
          "displayName": "Twitter for Android",
          "link": "http://twitter.com/download/android"
        },
        "body": "RT @LionelMedia: NYPD Commissioner Bratton vows to push against Apple, Google smartphone encryption http://something",
        "favoritesCount": 0,
        "objectType": "activity",
        "actor": {
          "summary": "4|\\|uVv@ kN0d3 1n 73H |/|ëTz",
          "image": "https://pbs.twimg.com/profile_images/505216574781792256/lD-6Hg_K_normal.jpeg",
          "statusesCount": 226174,
          "utcOffset": null,
          "languages": [ "en" ],
          "preferredUsername": "CyMadD0x",
          "displayName": "Film The Police",
          "postedTime": "2011-12-03T02:06:28.000Z",
          "link": "http://www.twitter.com/CyMadD0x",
          "verified": false,
          "friendsCount": 3918,
          "twitterTimeZone": null,
          "favoritesCount": 62255,
          "listedCount": 167,
          "objectType": "person",
          "links": [
            {
              "rel": "me",
              "href": null
            }
          ],
          "id": "id:twitter.com:427062600",
          "followersCount": 3666
        },
        "provider": {
          "displayName": "Twitter",
          "link": "http://www.twitter.com",
          "objectType": "service"
        },
        "twitter_filter_level": "medium",
        "twitter_entities": {
          "urls": [
            {
              "display_url": "something/76V2IBVi9",
              "indices": [ 100, 122 ],
              "expanded_url": "http://something/76V2IBVi9",
              "url": "http://something"
            }
          ],
          "hashtags": [ ],
          "user_mentions": [
            {
              "indices": [ 3, 15 ],
              "screen_name": "LionelMedia",
              "id_str": "24787367",
              "name": "Lionel",
              "id": 24787367
            }
          ],
          "trends": [ ],
          "symbols": [ ]
        },
        "twitter_lang": "en",
        "id": "tag:search.twitter.com,2005:523486878075744257",
        "retweetCount": 1,
        "gnip": {
          "urls": [
            {
              "expanded_url": "http://www.nydailynews.com/new-york/nypd-bratton-vows-push-apple-google-smartphone-encryption-article-1.1978719",
              "expanded_status": 200,
              "url": "http://seomthing"
            }
          ],
          "language": { "value": "fr" }
        },
        "object": {
          "postedTime": "2014-10-18T14:48:39.000Z",
          "verb": "post",
          "link": "http://twitter.com/LionelMedia/statuses/523485817625657344",
          "generator": {
            "displayName": "Facebook",
            "link": "http://www.facebook.com/twitter"
          },
          "body": "NYPD Commissioner Bratton vows to push against Apple, Google smartphone encryption http://something",
          "favoritesCount": 0,
          "objectType": "activity",
          "actor": {
            "summary": "LOTUS (Lionel of the U.S.) - The bravest man in media. Renaissance lawyer, vegan, news decoder, conspiracy analyst not theorist. DM: lionel@lionelmedia.com",
            "image": "https://pbs.twimg.com/profile_images/1872109972/LIONEL_HEADSHOT_normal",
            "statusesCount": 125171,
            "utcOffset": "-14400",
            "languages": [ "en" ],
            "preferredUsername": "LionelMedia",
            "displayName": "Lionel",
            "postedTime": "2009-03-16T22:45:21.000Z",
            "link": "http://www.twitter.com/LionelMedia",
            "verified": false,
            "friendsCount": 581,
            "twitterTimeZone": "Eastern Time (US & Canada)",
            "favoritesCount": 216,
            "listedCount": 239,
            "objectType": "person",
            "links": [
              {
                "rel": "me",
                "href": "http://lionelmedia.com"
              }
            ],
            "location": {
              "displayName": "Hell's Kitchen, USA",
              "objectType": "place"
            },
            "id": "id:twitter.com:24787367",
            "followersCount": 9951
          },
          "provider": {
            "displayName": "Twitter",
            "link": "http://www.twitter.com",
            "objectType": "service"
          },
          "twitter_filter_level": "low",
          "twitter_entities": {
            "urls": [
              {
                "display_url": "something/76V2IBVi9",
                "indices": [ 83, 105 ],
                "expanded_url": "http://something/76V2IBVi9",
                "url": "http://something"
              }
            ],
            "hashtags": [ ],
            "user_mentions": [ ],
            "trends": [ ],
            "symbols": [ ]
          },
          "twitter_lang": "en",
          "id": "tag:search.twitter.com,2005:523485817625657344",
          "object": {
            "summary": "NYPD Commissioner Bratton vows to push against Apple, Google smartphone encryption http://something",
            "postedTime": "2014-10-18T14:48:39.000Z",
            "link": "http://twitter.com/LionelMedia/statuses/523485817625657344",
            "id": "object:search.twitter.com,2005:523485817625657344",
            "objectType": "note"
          }
        }
      }
    },
    {
      "cde": {
        "author": {
          "gender": "male",
          "parenthood": {
            "isParent": "unknown",
            "evidence": ""
          },
          "location": {
            "country": "Ireland",
            "city": "",
            "state": ""
          },
          "maritalStatus": {
            "isMarried": "unknown",
            "evidence": ""
          }
        },
        "content": {
          "sentiment": {
            "evidence": [
              {
                "polarity": "NEGATIVE",
                "sentimentTerm": "stereotype"
              }
            ],
            "polarity": "NEGATIVE"
          }
        }
      },
      "cdeInternal": { "tracks": [ { "id": "2713720a-f341-4ca0-acd1-f8b1e6c0d00f" } ] },
      "message": {
        "twitter_extended_entities": {
          "media": [
            {
              "display_url": "pic.twitter.com/JwXAJbPxms",
              "indices": [ 104, 126 ],
              "sizes": {
                "small": {
                  "w": 340,
                  "h": 305,
                  "resize": "fit"
                },
                "large": {
                  "w": 1024,
                  "h": 918,
                  "resize": "fit"
                },
                "thumb": {
                  "w": 150,
                  "h": 150,
                  "resize": "crop"
                },
                "medium": {
                  "w": 600,
                  "h": 538,
                  "resize": "fit"
                }
              },
              "id_str": "523494030517084160",
              "expanded_url": "http://twitter.com/theapplefarmer/status/523494051019235328/photo/1",
              "source_status_id_str": "523494051019235328",
              "media_url_https": "https://pbs.twimg.com/media/B0PTDBMCIAAcd61.jpg",
              "id": 523494030517084160,
              "type": "photo",
              "media_url": "http://pbs.twimg.com/media/B0PTDBMCIAAcd61.jpg",
              "source_status_id": 523494051019235328,
              "url": "http://something"
            }
          ]
        },
        "postedTime": "2014-10-18T15:48:33.000Z",
        "verb": "share",
        "link": "http://twitter.com/McKennasGuides/statuses/523500890398154752",
        "generator": {
          "displayName": "Twitter for iPhone",
          "link": "http://twitter.com/download/iphone"
        },
        "body": "RT @theapplefarmer: Elstar apples now on sale. Cue stereotype picture of an apple with leaf attached... http://something",
        "favoritesCount": 0,
        "objectType": "activity",
        "actor": {
          "summary": "Sally tweets.",
          "image": "https://pbs.twimg.com/profile_images/378800000682481838/0dc56479e41d25fa6c4a9056c351f866_normal.jpeg",
          "statusesCount": 12203,
          "utcOffset": "3600",
          "languages": [ "en" ],
          "preferredUsername": "McKennasGuides",
          "displayName": "John & Sally McKenna",
          "postedTime": "2010-03-10T15:38:14.000Z",
          "link": "http://www.twitter.com/McKennasGuides",
          "verified": false,
          "friendsCount": 1148,
          "twitterTimeZone": "Dublin",
          "favoritesCount": 982,
          "listedCount": 131,
          "objectType": "person",
          "links": [
            {
              "rel": "me",
              "href": "http://www.guides.ie"
            }
          ],
          "location": {
            "displayName": "Ireland",
            "objectType": "place"
          },
          "id": "id:twitter.com:121784956",
          "followersCount": 12032
        },
        "provider": {
          "displayName": "Twitter",
          "link": "http://www.twitter.com",
          "objectType": "service"
        },
        "twitter_filter_level": "medium",
        "twitter_entities": {
          "urls": [ ],
          "hashtags": [ ],
          "media": [
            {
              "display_url": "pic.twitter.com/JwXAJbPxms",
              "indices": [ 104, 126 ],
              "sizes": {
                "small": {
                  "w": 340,
                  "h": 305,
                  "resize": "fit"
                },
                "large": {
                  "w": 1024,
                  "h": 918,
                  "resize": "fit"
                },
                "thumb": {
                  "w": 150,
                  "h": 150,
                  "resize": "crop"
                },
                "medium": {
                  "w": 600,
                  "h": 538,
                  "resize": "fit"
                }
              },
              "id_str": "523494030517084160",
              "expanded_url": "http://twitter.com/theapplefarmer/status/523494051019235328/photo/1",
              "source_status_id_str": "523494051019235328",
              "media_url_https": "https://pbs.twimg.com/media/B0PTDBMCIAAcd61.jpg",
              "id": 523494030517084160,
              "type": "photo",
              "media_url": "http://pbs.twimg.com/media/B0PTDBMCIAAcd61.jpg",
              "source_status_id": 523494051019235328,
              "url": "http://something"
            }
          ],
          "user_mentions": [
            {
              "indices": [ 3, 18 ],
              "screen_name": "theapplefarmer",
              "id_str": "303651044",
              "name": "Cornelius Traas",
              "id": 303651044
            }
          ],
          "trends": [ ],
          "symbols": [ ]
        },
        "twitter_lang": "en",
        "id": "tag:search.twitter.com,2005:523500890398154752",
        "retweetCount": 1,
        "gnip": {
          "urls": [
            {
              "expanded_url": "http://twitter.com/theapplefarmer/status/523494051019235328/photo/1",
              "expanded_status": 200,
              "url": "http://something"
            }
          ],
          "profileLocations": [
            {
              "geo": {
                "coordinates": [ -8.0, 53.0 ],
                "type": "point"
              },
              "address": {
                "country": "Ireland",
                "countryCode": "IE"
              },
              "displayName": "Ireland",
              "objectType": "place"
            }
          ],
          "language": { "value": "en" }
        },
        "object": {
          "twitter_extended_entities": {
            "media": [
              {
                "display_url": "pic.twitter.com/JwXAJbPxms",
                "indices": [ 84, 106 ],
                "sizes": {
                  "small": {
                    "w": 340,
                    "h": 305,
                    "resize": "fit"
                  },
                  "large": {
                    "w": 1024,
                    "h": 918,
                    "resize": "fit"
                  },
                  "thumb": {
                    "w": 150,
                    "h": 150,
                    "resize": "crop"
                  },
                  "medium": {
                    "w": 600,
                    "h": 538,
                    "resize": "fit"
                  }
                },
                "id_str": "523494030517084160",
                "expanded_url": "http://twitter.com/theapplefarmer/status/523494051019235328/photo/1",
                "media_url_https": "https://pbs.twimg.com/media/B0PTDBMCIAAcd61.jpg",
                "id": 523494030517084160,
                "type": "photo",
                "media_url": "http://pbs.twimg.com/media/B0PTDBMCIAAcd61.jpg",
                "url": "http://something"
              }
            ]
          },
          "postedTime": "2014-10-18T15:21:22.000Z",
          "verb": "post",
          "link": "http://twitter.com/theapplefarmer/statuses/523494051019235328",
          "generator": {
            "displayName": "Twitter for Android",
            "link": "http://twitter.com/download/android"
          },
          "body": "Elstar apples now on sale. Cue stereotype picture of an apple with leaf attached... http://something/JwXAJbPxms",
          "favoritesCount": 3,
          "objectType": "activity",
          "actor": {
            "summary": "Fruit grower, juice maker, university lecturer, horticultural scientist.",
            "image": "https://pbs.twimg.com/profile_images/461130678499676160/iIGjV61N_normal.jpeg",
            "statusesCount": 7952,
            "utcOffset": "3600",
            "languages": [ "en" ],
            "preferredUsername": "theapplefarmer",
            "displayName": "Cornelius Traas",
            "postedTime": "2011-05-23T07:27:59.000Z",
            "link": "http://www.twitter.com/theapplefarmer",
            "verified": false,
            "friendsCount": 2147,
            "twitterTimeZone": "Dublin",
            "favoritesCount": 236,
            "listedCount": 65,
            "objectType": "person",
            "links": [
              {
                "rel": "me",
                "href": "http://www.theapplefarm.com"
              }
            ],
            "location": {
              "displayName": "Tipperary, Ireland",
              "objectType": "place"
            },
            "id": "id:twitter.com:303651044",
            "followersCount": 2569
          },
          "provider": {
            "displayName": "Twitter",
            "link": "http://www.twitter.com",
            "objectType": "service"
          },
          "twitter_filter_level": "low",
          "twitter_entities": {
            "urls": [ ],
            "hashtags": [ ],
            "media": [
              {
                "display_url": "pic.twitter.com/JwXAJbPxms",
                "indices": [ 84, 106 ],
                "sizes": {
                  "small": {
                    "w": 340,
                    "h": 305,
                    "resize": "fit"
                  },
                  "large": {
                    "w": 1024,
                    "h": 918,
                    "resize": "fit"
                  },
                  "thumb": {
                    "w": 150,
                    "h": 150,
                    "resize": "crop"
                  },
                  "medium": {
                    "w": 600,
                    "h": 538,
                    "resize": "fit"
                  }
                },
                "id_str": "523494030517084160",
                "expanded_url": "http://twitter.com/theapplefarmer/status/523494051019235328/photo/1",
                "media_url_https": "https://pbs.twimg.com/media/B0PTDBMCIAAcd61.jpg",
                "id": 523494030517084160,
                "type": "photo",
                "media_url": "http://pbs.twimg.com/media/B0PTDBMCIAAcd61.jpg",
                "url": "http://something/JwXAJbPxms"
              }
            ],
            "user_mentions": [ ],
            "trends": [ ],
            "symbols": [ ]
          },
          "twitter_lang": "en",
          "location": {
            "geo": {
              "coordinates": [
                [
                  [ -8.4057899, 52.2013149 ],
                  [ -8.4057899, 52.7335849 ],
                  [ -7.3718273, 52.7335849 ],
                  [ -7.3718273, 52.2013149 ]
                ]
              ],
              "type": "Polygon"
            },
            "country_code": "Ireland",
            "displayName": "South Tipperary",
            "name": "South Tipperary",
            "twitter_country_code": "IE",
            "link": "https://api.twitter.com/1.1/geo/id/2edf98027d556c5f.json",
            "objectType": "place",
            "twitter_place_type": "city"
          },
          "id": "tag:search.twitter.com,2005:523494051019235328",
          "object": {
            "summary": "Elstar apples now on sale. Cue stereotype picture of an apple with leaf attached... http://something/JwXAJbPxms",
            "postedTime": "2014-10-18T15:21:22.000Z",
            "link": "http://twitter.com/theapplefarmer/statuses/523494051019235328",
            "id": "object:search.twitter.com,2005:523494051019235328",
            "objectType": "note"
          }
        }
      }
    }
  ]
}
}
}
]
}

0 个答案:

没有答案