簡體   English   中英

如何從URL檢索Json作為字符串?

[英]how to retrieve Json as string from url?

我有一個返回JSON的URL。 我正在Windows Phone中開發一個應用程序,我想通過發送url獲取該json數據,然后將其轉換為字符串並從中提取數據。 我已經下載了json.net來讀取json數據。
我是否需要對數據進行反序列化。
我不能只獲取字符串格式的數據然后進行解析。

我正在使用以下代碼讀取字符串格式的json數據。

Array Movies = jObject["movies"].ToArray();
foreach (JToken Movie in Movies)
    MessageBox.Show(Movie["title"].ToString());

json看起來像這樣:

{"total":51,"movies":[{"id":"771242005","title":"Magic Mike","year":2012,"mpaa_rating":"R","runtime":110,"critics_consensus":"Magic Mike's sensitive direction, smart screenplay, and strong performances allow audiences to have their beefcake and eat it too.","release_dates":{"theater":"2012-06-29","dvd":"2012-10-23"},"ratings":{"critics_rating":"Certified Fresh","critics_score":79,"audience_rating":"Upright","audience_score":63},"synopsis":"Set in the world of male strippers, Magic Mike is directed by Steven Soderbergh and stars Channing Tatum in a story inspired by his real life. The film follows Mike (Tatum) as he takes a young dancer called The Kid (Pettyfer) under his wing and schools him in the fine arts of partying, picking up women, and making easy money. -- (C) Warner Bros.","posters":{"thumbnail":"http://content8.flixster.com/movie/11/16/66/11166610_mob.jpg","profile":"http://content8.flixster.com/movie/11/16/66/11166610_pro.jpg","detailed":"http://content8.flixster.com/movie/11/16/66/11166610_det.jpg","original":"http://content8.flixster.com/movie/11/16/66/11166610_ori.jpg"},"abridged_cast":[{"name":"Channing Tatum","id":"162661835","characters":["Magic Mike"]},{"name":"Alex Pettyfer","id":"326298019","characters":["Adam","The Kid"]},{"name":"Matt Bomer","id":"771077752","characters":["Ken"]},{"name":"Joe Manganiello","id":"770800475","characters":["Big Dick Richie"]},{"name":"Matthew McConaughey","id":"162652350","characters":["Dallas"]}],"alternate_ids":{"imdb":"1915581"},"links":{"self":"http://api.rottentomatoes.com/api/public/v1.0/movies/771242005.json","alternate":"http://www.rottentomatoes.com/m/magic_mike/","cast":"http://api.rottentomatoes.com/api/public/v1.0/movies/771242005/cast.json","clips":"http://api.rottentomatoes.com/api/public/v1.0/movies/771242005/clips.json","reviews":"http://api.rottentomatoes.com/api/public/v1.0/movies/771242005/reviews.json","similar":"http://api.rottentomatoes.com/api/public/v1.0/movies/771242005/similar.json"}}],"links":{"self":"http://api.rottentomatoes.com/api/public/v1.0/lists/dvds/upcoming.json?page_limit=1&country=us&page=1","next":"http://api.rottentomatoes.com/api/public/v1.0/lists/dvds/upcoming.json?page_limit=1&country=us&page=2","alternate":"http://www.rottentomatoes.com/dvd/upcoming.json"},"link_template":"http://api.rottentomatoes.com/api/public/v1.0/lists/dvds/upcoming.json?page_limit={results-per-page}&page={page-number}&country={country-code}"}

`

我只想要電影名稱和標題。

如果您的問題是“是否需要反序列化”,那么答案是否定的,那就沒有必要。

也就是說,與數據服務器端一起使用時,通常首選將JSON字符串反序列化為強類型對象,因為這可以減少運行時錯誤並更輕松地測試代碼。

使用JSON.Net的示例:

public class Movie
{
    public string Name { get; set; }
    public string Title { get; set; }
}

public Movie Convert(string jsonString)
{
    return JsonConvert.DeserializeObject<Movie>(jsonString);
}

還有其他用於將JSON反序列化為對象的選項。 如果您有一個表示JSON對象的DataContract,則可以使用DataContractJsonSerializer

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM