使用JSON.NET从JSON中获取值

37

我试图使用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);

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

3个回答

81

这应该会对你有所帮助: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"];

4
你应该测试一下这个键是否存在:o.ContainsKey("Name") - Mo Zaatar

3

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

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

// Ex. output 
object.Name;

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接