JSON Problem

J

James

Hi, My code is below. I am not able to extract the 'name' and 'query' lists
from the JSON via a DataContracted Class (below)
I have spent a long time trying to work this one out, and could really do
with some help...

My Json == {"as_of":1266853488,"trends":{"2010-02-22
15:44:48":[{"name":"#nowplaying","query":"#nowplaying"},{"name":"#musicmonday","query":"#musicmonday"},{"name":"#WeGoTogetherLike","query":"#WeGoTogetherLike"},{"name":"#imcurious","query":"#imcurious"},{"name":"#mm","query":"#mm"},{"name":"#HumanoidCityTour","query":"#HumanoidCityTour"},{"name":"#awesomeindianthings","query":"#awesomeindianthings"},{"name":"#officeformac","query":"#officeformac"},{"name":"Justin
Bieber","query":"\"Justin Bieber\""},{"name":"National
Margarita","query":"\"National Margarita\""}]}}


WebClient wc = new WebClient();
wc.Credentials = new NetworkCredential(this.Auth.UserName,
this.Auth.Password);
string res = wc.DownloadString(new Uri(link));
//the download string gives me the above JSON string - no
problems

Trends trends = new Trends();

Trends obj = Deserialise<Trends>(res);



private T Deserialise<T>(string json)
{

T obj = Activator.CreateInstance<T>();
using (MemoryStream ms = new
MemoryStream(Encoding.Unicode.GetBytes(json)))
{
DataContractJsonSerializer serialiser = new
DataContractJsonSerializer(obj.GetType());
obj = (T)serialiser.ReadObject(ms);
ms.Close();
return obj;
}
}


[DataContract]
public class Trends //: List<Trend>
{
[DataMember(Name = "as_of")]
public string AsOf { get; set; }

//The As_OF value is returned - But how do I get the
multidimensional array of Names and Queries from the JSON here?
[DataMember(Name = "trends")]
public Dictionary<string, string> Items { get; set; }


}
 
R

Rich P

Json issues are usually a web.config based problem. You are not
referencing the correct libraries in your web.config file for some third
party libary you are using in your project. If you are using a third
party library and having this json issue it is because you are probably
using an outdated web.config file for it. You need to go to the vendor
and get the updated library references.


Rich
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top