2012-09-03 72 views
0

好的,我會盡力在我的問題非常具體。經過一些研究後,我終於使我的代碼工作(種類,因爲它不會返回所需的結果)。我目前正在使用JSON.net並嘗試反序列化以下Json字符串,這是對Twitter API的響應。正確的Json反序列化

[{"created_at":"2012-09-03T18:22:54Z","locations":[{"name":"Globales","woeid":1}],"trends":[{"query":"%2327CosasSobreMi","name":"#27CosasSobreMi","promoted_content":null,"url":"http:\/\/twitter.com\/search\/?q=%2327CosasSobreMi","events":null},{"query":"%23AskTravis","name":"#AskTravis","promoted_content":null,"url":"http:\/\/twitter.com\/search\/?q=%23AskTravis","events":null},{"query":"%23WhyDoPeopleThinkItsOkayTo","name":"#WhyDoPeopleThinkItsOkayTo","promoted_content":null,"url":"http:\/\/twitter.com\/search\/?q=%23WhyDoPeopleThinkItsOkayTo","events":null},{"query":"%22We%20%3C3%20Justin%22","name":"We \u003C3 Justin","promoted_content":null,"url":"http:\/\/twitter.com\/search\/?q=%22We%20%3C3%20Justin%22","events":null},{"query":"%22We%20Adore%20One%20Direction%22","name":"We Adore One Direction","promoted_content":null,"url":"http:\/\/twitter.com\/search\/?q=%22We%20Adore%20One%20Direction%22","events":null},{"query":"%22Stefan%20Is%20Elena's%20Humanity%22","name":"Stefan Is Elena's Humanity","promoted_content":null,"url":"http:\/\/twitter.com\/search\/?q=%22Stefan%20Is%20Elena's%20Humanity%22","events":null},{"query":"%22Eric%20Saade%20Come%20Back%20To%20Poland%22","name":"Eric Saade Come Back To Poland","promoted_content":null,"url":"http:\/\/twitter.com\/search\/?q=%22Eric%20Saade%20Come%20Back%20To%20Poland%22","events":null},{"query":"Hlavackova","name":"Hlavackova","promoted_content":null,"url":"http:\/\/twitter.com\/search\/?q=Hlavackova","events":null},{"query":"%22Serena%20Williams%22","name":"Serena Williams","promoted_content":null,"url":"http:\/\/twitter.com\/search\/?q=%22Serena%20Williams%22","events":null},{"query":"%22Kire%C3%A7burnu%20%C3%87akallar%C4%B1%22","name":"Kire\u00e7burnu \u00c7akallar\u0131","promoted_content":null,"url":"http:\/\/twitter.com\/search\/?q=%22Kire%C3%A7burnu%20%C3%87akallar%C4%B1%22","events":null}],"as_of":"2012-09-03T18:25:18Z"}] 

普通的Json包含對象和東西......除了我不知道跟第一次「[,]」的開始和結束,有朋友指出這一點有什麼字符串。現在,我做了一些類對於JSON字符串:

public class Location 
{ 
    private string _name; 
    private int _woeid; 

    public string name { get { return _name; } set { value = _name; } } 
    public int woeid { get { return _woeid; } set { value = _woeid; } } 
} 

public class Trend 
{ 
    private string _query, _name, _url; 
    private object _promoted_content, _events; 

    public string query { get { return _query; } set { value = _query; } }   
    public string name { get { return _name; } set { value = _name; } } 
    public object promoted_content { get { return _promoted_content; } set { value = _promoted_content; } } 
    public string url { get { return _url; } set { value = _url; } } 
    public object events { get { return _events; } set { value = _events; } } 

} 

public class RootObject 
{ 
    private List<Location> _locations; 
    private List<Trend> _trends; 
    private string created_at_, _as_of; 

    public List<Trend> trends { get { return _trends; } set { value = _trends; } }   
    public string created_at { get { return created_at_;} set { value = created_at_; } } 
    public string as_of { get { return _as_of ;} set { value = _as_of; } } 
    public List<Location> locations { get { return _locations; } set { value = _locations; }} 
}  

而且我使用deserealize它的方法,這是一個:

WebClient client = new WebClient(); 
Stream stream = client.OpenRead(@"https://api.twitter.com/1/trends/1.json"); 
StreamReader reader = new StreamReader(stream); 
string nvm = reader.ReadToEnd(); 
try 
{ 
    List<RootObject> content = JsonConvert.DeserializeObject<List<RootObject>>(JsonString); 
} 
catch (System.Exception message) 
{ 
    throw; 
} 

我實在不明白,如果我的類是錯誤的,因爲變量內容總是空的,我不能改變抽象定義爲使用Visual Studio 2005即時通訊。試圖改變類內的列表,但不能使其工作。

我已經嘗試了不同的方法,所有不同的響應在這裏在StackOverflow中,但他們似乎都使用一些方法不可用或我經常反序列化爲他們工作。

+0

提示:永遠不要嘗試/趕上/拋出。只需刪除相同效果的try/catch即可。 –

+0

是的,那裏有一些錯誤處理邏輯,但我刪除了代碼以使它簡短。謝謝你的提示。 –

回答

5

你不需要任何這些類。只需使用dynamic

WebClient client = new WebClient(); 
string json = client.DownloadString("https://api.twitter.com/1/trends/1.json"); 

dynamic dynObj = JsonConvert.DeserializeObject(json); 

foreach (var item in dynObj) 
{ 
    foreach (var trend in item.trends) 
    { 
     Console.WriteLine("{0} - {1} - {2}",trend.query,trend.name,trend.url); 
    } 
} 

編輯

另一個版本沒有dynamic

var dynObj = (JArray)JsonConvert.DeserializeObject(json); 
foreach (var item in dynObj) 
{ 
    foreach (var trend in item["trends"]) 
    { 
     Console.WriteLine("{0} - {1} - {2}",trend["query"],trend["name"],trend["url"]); 
    } 
} 

EDIT2

JArray dynObj = (JArray)JsonConvert.DeserializeObject(json); 
foreach (JObject item in dynObj) 
{ 
    foreach (JObject trend in item["trends"]) 
    { 
     Console.WriteLine("{0} - {1} - {2}",trend["query"],trend["name"],trend["url"]); 
    } 
} 

編輯3

for (int i = 0; i < dynObj.Count;i++) 
{ 
    for (int j = 0; j < ((JArray) dynObj[i]["trends"]).Count;j++) 
    { 
     JObject trend = (JObject)((JArray)dynObj[i]["trends"])[j]; 
     Console.WriteLine("{0} - {1} - {2}", trend["query"], trend["name"], trend["url"]); 
    } 
} 
+0

我知道這種方法,但即時通訊使用舊版本的C#和我相信「動態」的聲明不可用,任何方式來取代它?還是說我沒有使用合適的裝配參考? –

+0

@EdwardNewgate動態關鍵字附帶Visual C#2010,請參閱編輯。 –

+0

感謝您使用這些方法,但我仍然無法在VS 2005中使用var,我知道它很老,但這些是這個應用程序開發者的必備條件,我知道您可以使用「Label」替換var,但不知道如何去做正確地,我會盡力讓代碼與我有的工作,但我可以使用其他編輯。 =)非常感謝你的時間。 –