2012-01-25 69 views
22

我尝试使用http://www.codeplex.com/Json从json对象提取值。使用JSON.NET从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); 

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

回答

54

这会帮助你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"];