[通过JSON.NET从JSON获取值

问题描述 投票:32回答:3

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

我使用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 "}

我如何获得这样的标题,等级,年份?并保存到我的对象中?

此行返回正确的json:

JObject jObject = JObject.Parse (json);

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

c# json
3个回答
70
投票

这应该对您有帮助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"];

2
投票

JObject API documentation

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

Full Documentation


0
投票
class TypeHere{
   string Name {get;set;}
}

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

// Ex. output 
object.Name;
© www.soinside.com 2019 - 2024. All rights reserved.