C# 使用 JSON.NET 从 JSON 中获取价值

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/9010021/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-09 05:35:10  来源:igfitidea点击:

Get value from JSON with JSON.NET

c#json

提问by SteelSoft

I try to use http://www.codeplex.com/Jsonto extract values ??from a json object.

我尝试使用http://www.codeplex.com/Json从 json 对象中提取值?

I use imdbapi.com and they return json like this:

我使用 imdbapi.com,他们像这样返回 json:

{"Title": "Star Wars", "Year": "1977", "Rated", "PG", "Released", "25 May 1977", "Genre", "Action, Adventure, Fantasy, Sci-Fi "" Director ":" George Lucas "," Writer "," George Lucas "," Actors ":" Mark Hamill, Harrison Ford, Carrie Fisher, Alec Guinness, "" Plot ":" Luke Skywalker leaves his home planet, teams up With Other Rebels, and Tries to save Princess Leia from the evil clutch of Darth hrs 1 min "," Rating ":" 8.8 "," Votes ":" 397318 "," ID ":" tt0076759 "," Response ":" True "}

How can I pick up such title, rating, Year? and save it to my object?

我怎样才能获得这样的称号、评级、年份?并将其保存到我的对象?

This line return correct json:

这一行返回正确的 json:

JObject jObject = JObject.Parse (json);

Now I just need help picking out the data I want. any suggestions?

现在我只需要帮助挑选我想要的数据。有什么建议?

回答by Michael

This should help you http://james.newtonking.com/pages/json-net.aspx

这应该可以帮助您http://james.newtonking.com/pages/json-net.aspx

string json = @"{
    ""Name"": ""Apple"",
    ""Expiry"": new Date(1230422400000),
    ""Price"": 3.99,
    ""Sizes"": [
        ""Small"",
        ""Medium"",
        ""Large""
    ]
}";

JObject o = JObject.Parse(json);

//This will be "Apple"
string name = (string)o["Name"];

回答by Abdul Hfuda

JObject API documentation

JObject API 文档

I believe you are interested in the .Item[key] collection that returns JTokens.

我相信您对返回 JToken 的 .Item[key] 集合感兴趣。

Full Documentation

完整文档

回答by Sean Zamora

class TypeHere{
   string Name {get;set;}
}

TypeHere object = JsonConvert.DeserializeObject< TypeHere >(jsonString)

// Ex. output 
object.Name;