好的,我会在这里尝试非常具体地解决我的问题。经过一些研究,我终于使我的代码工作(有点,因为它没有返回所需的结果)。我目前正在使用 JSON.net 并尝试反序列化以下 Json 字符串,该字符串是对 twitter API 的响应。
[{"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; }}
}
我用来解除现实的方法是这样的:
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;
}
我只是无法确定我的类是否错误,因为变量内容始终为 Null,并且我无法像使用 Visual Studio 2005 那样更改抽象定义。尝试更改类中的列表但无法使其工作。
我在 StackOverflow 中尝试了所有不同响应的不同方法,但它们似乎都使用了一些我无法使用的方法,或者常规反序列化对它们有效。