Parsing JSON with JSON.NET

穿精又带淫゛_ 提交于 2019-12-06 08:15:11
s_hewitt

Have you tried things like?

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"];

What exactly are you trying to get into the name variable?

Using Json.Net, you can deserialize the object like this:

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

You would define the BlogSearch object like this:

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

You keep defining objects until you have all the ones you are interested in.

http://msdn.microsoft.com/en-us/library/system.runtime.serialization.json.datacontractjsonserializer.aspx
Its a good alternative to your method, that I would recommend...
Hope that helps...

peteb

If you're posting your JSON object raw to the Web API then you will run into this problem. The Deserializer is expecting an actual string and not an object or an array. Because it is using a JsonMediaTypeFormatter, it won't know how to translate what is being passed to it.

You need to do the following to avoid the null:

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

For more information see this article.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!