Okay I’ll try to be very specific with my problem right here. After some research i finally made my code work (kind of, because it doesn’t return the desired result). I’m currently using JSON.net and try to deserialize the following Json string which is a response for a 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"}]
Regular Json string containing objects and stuff… Except i don’t know what’s with the first “[,]” in the beginning and in the end, a friend pointed that out. Now i made some classes for that Json string:
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; }}
}
And the method I’m using to deserealize it is this one:
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;
}
I just can’t figure if my classes are wrong because the variable content is always Null and i can’t change the abstract definitions as im using Visual Studio 2005. Tried changing the Lists inside the classes but can’t make it work.
I’ve tried different methods with all the different responses here in StackOverflow but they all seem to either use some methods unavailable for me or the regular deserialization works for them.
You don’t need any of these classes. Just use
dynamicEDIT
Another version without
dynamicEDIT2
EDIT3