2
首先,這已被問了很多次,但讀過我發現的所有帖子都沒有提供一個解決我特定情況的答案。如何使用JSON.net格式化嵌套JSON對象的輸出?
另外,請原諒任何不正確的術語,因爲我可能會濫用條款...
我試圖從該查詢採取JSON和簡單地輸出到文本塊:
http://www.imdb.com/xml/find?json=1&nr=1&nm=on&q=jeniffer+garner
其產生此:
{
"name_approx":[
{
"id":"nm0004950",
"title":"",
"name":"Jennifer Garner",
"description":"Actress, Dallas Buyers Club"
},
//more code
{
"id":"nm3144518",
"title":"",
"name":"Jennifer Varner",
"description":"Self, THS Investigates: Hot for Student"
}]}
我試圖使用的代碼如下。
類:
public class Movie
{
public List<Stream> name_approx { get; set; }
public Movie()
{}
}
public class Stream
{
public string id { get; set; }
public string title { get; set; }
public string name { get; set; }
public string description { get; set; }
public Stream()
{}
}
和...
searchOutput.Text = "";
searchStatusOutput.Text = "Awaiting Response...";
string userURI = inputAddress.Text;
var response = await httpClient.GetAsync(userURI);
response.EnsureSuccessStatusCode();
var content = await response.Content.ReadAsStringAsync();
Movie output = JsonConvert.DeserializeObject<Movie>(content);
//searchOutput.Text = ??????
當我運行此我可以看到,影片輸出正確包含一個'name_approx'對象,並嵌套在20如我所料,'Streams'。
我不知道如何輸出這個到我的文本塊。我嘗試了很多方法,並認爲我需要使用某種形式的foreach,但是我卡住了,無法解決。
太感謝你了,我一直在嘗試3小時,該做我自己之前,我張貼在這裏。 – James 2014-11-23 16:24:43
不用擔心隊友,如果你打算使用這個庫,請查看文檔,因爲它非常強大。 – 2014-11-23 16:39:04