3

我有一个 JSON 字符串:

 {"responseData": 
     {"results": [
         {"GsearchResultClass": "GblogSearch",
          "title":"\u003cb\u003eParis Hilton\u003c/b\u003e shops at Sydney Michelle boutique in the Beverly Glen \u003cb\u003e...\u003c/b\u003e",
           "titleNoFormatting":"Paris Hilton shops at Sydney Michelle boutique in the Beverly Glen ...",
           "postUrl":"http://www.celebrity-gossip.net/celebrities/hollywood/paris-hilton-sydney-michelle-stockup-215844/",
           "content":"\u003cb\u003eParis Hilton\u003c/b\u003e shops at Sydney Michelle boutique in the Beverly Glen Mall - \u003cb\u003eParis Hilton\u003c/b\u003e: Sydney Michelle Stockup.",
           "author":"The Gossip Girls at (c) gossipgirls.com",
           "blogUrl":"http://www.celebrity-gossip.net/",
           "publishedDate":"Tue, 23 Feb 2010 22:26:00 -0800"
         },
         {"GsearchResultClass":"GblogSearch",
          "title":"\u003cb\u003eParis Hilton\u003c/b\u003e having wardrobe woes as she met with her lawyer",
          "titleNoFormatting":"Paris Hilton having wardrobe woes as she met with her lawyer",
          "postUrl":"http://www.celebrity-gossip.net/celebrities/hollywood/paris-hiltons-wardrobe-woes-215855/",
          "content":"\u003cb\u003eParis Hilton\u003c/b\u003e having wardrobe woes as  she met with her lawyer - \u003cb\u003eParis Hilton's\u003c/b\u003e Wardrobe Woes.",
          "author":"The Gossip Girls at (c) gossipgirls.com","blogUrl":"http://www.celebrity-gossip.net/",
          "publishedDate":"Wed, 24 Feb 2010 11:07:56 -0800"
         },
         {"GsearchResultClass":"GblogSearch",
          "title":"HOT GALLERY: \u003cb\u003eParis Hilton\u003c/b\u003e Turns Her Frown Upside Down | OK \u003cb\u003e...\u003c/b\u003e",
          "titleNoFormatting":"HOT GALLERY: Paris Hilton Turns Her Frown Upside Down | OK ...",
          "postUrl":"http://www.okmagazine.com/2010/02/hot-gallery-paris-hilton-turns-her-frown-upside-down/",
          "content":"\u003cb\u003eParis Hilton\u003c/b\u003e kept her game face on yesterday as she headed to a meeting in Hollywood. The socialite maintained her composure, but eventually cracked a smile, 201002.",
          "author":"Brittany Talarico",
          "blogUrl":"http://www.okmagazine.com/",
          "publishedDate":"Wed, 24 Feb 2010 07:57:10 -0800"
         },
         {"GsearchResultClass":"GblogSearch",
          "title":"Love It Or Hate It: \u003cb\u003eParis Hilton\u003c/b\u003e | ImNotObsessed.com",
          "titleNoFormatting":"Love It Or Hate It: Paris Hilton | ImNotObsessed.com",
          "postUrl":"http://www.imnotobsessed.com/2010/02/24/love-it-or-hate-it-paris-hilton",
          "content":"tweetmeme_url \u003d \"http://www.imnotobsessed.com/2010/02/24/love-it-or-hate-it-\u003cb\u003eparis\u003c/b\u003e-\u003cb\u003ehilton\u003c/b\u003e\";tweetmeme_element_id \u003d '#tweetmeme-widget-139430e62dc37d7a2aa71840d6444572';That's some dress \u003cb\u003eParis Hilton\u003c/b\u003e was seen wearing while shopping in ...",
          "author":"Vera",
          "blogUrl":"http://www.imnotobsessed.com/",
          "publishedDate":"Wed, 24 Feb 2010 10:44:28 -0800"
         }],
    "cursor": { 
        "pages": [
            {"start":"0","label":1},
            {"start":"4","label":2},
            {"start":"8","label":3},
            {"start":"12","label":4},
            {"start":"16","label":5},
            {"start":"20","label":6},
            {"start":"24","label":7},
            {"start":"28","label":8}],
        "estimatedResultCount":"8035445",
        "currentPageIndex":0,
        "moreResultsUrl":"http://blogsearch.google.com/blogsearch?oe\u003dutf8\u0026ie\u003dutf8\u0026safe\u003dactive\u0026source\u003duds\u0026start\u003d0\u0026hl\u003den\u0026q\u003dParis+Hilton"
        }
     }, 
"responseDetails": null, 
"responseStatus": 200}

编。注意:为可读性添加了换行符

我正在使用 Json.NET 来解析它,但是它给了我一个 null

这是我的代码:

JObject o = JObject.Parse(json); // <- where json is the string above

string name = (string)o["responseData"];

但它给了我这个错误:

无法将 {null} 转换为字符串。
4

4 回答 4

6

使用 Json.Net,您可以像这样反序列化对象:

BlogSearch search = JsonConvert.DeserializeObject<BlogSearch>(content);

您可以像这样定义 BlogSearch 对象:

[JsonObject(MemberSerialization.OptIn)]
public class BlogSearch
{
    [JsonProperty(PropertyName = "responseData")]
    public BlogSearchResponse SearchResponse { get; set; }
}

你不断定义对象,直到你拥有所有你感兴趣的对象。

于 2010-04-22T15:12:50.927 回答
5

你试过类似的东西吗?

string gsearchresultclass= (string)o["responseData"]["results"][0]["GsearchResultClass"];
string title= (string)o["responseData"]["results"][0]["title"];
string titlenoformat= (string)o["responseData"]["results"][0]["titleNoFormatting"];
string url = (string)o["responseData"]["results"][0]["postUrl"];
string content = (string)o["responseData"]["results"][0]["content"];
string author = (string)o["responseData"]["results"][0]["author"];
string blogurl = (string)o["responseData"]["results"][0]["blogUrl"];
string date = (string)o["responseData"]["results"][0]["publishedDate"];

你到底想进入 name 变量是什么?

于 2010-02-25T01:04:35.310 回答
1

http://msdn.microsoft.com/en-us/library/system.runtime.serialization.json.datacontractjsonserializer.aspx
它是您的方法的一个很好的替代方案,我会推荐...
希望有帮助...

于 2010-02-25T00:43:53.670 回答
0

如果您将原始 JSON 对象发布到 Web API,那么您将遇到此问题。Deserializer 需要一个实际的字符串,而不是对象或数组。因为它使用的是 a JsonMediaTypeFormatter,所以它不知道如何翻译传递给它的内容。

您需要执行以下操作以避免null

public HttpResponseMessage postBlogSearch([FromBody] JToken json){
    var jsonResult = JObject.Parse(json.ToString());
    var name = jsonResult["responseData"].ToString();
}

有关详细信息,请参阅本文

于 2014-10-21T16:32:15.747 回答