-
-
Save waema/973510 to your computer and use it in GitHub Desktop.
In response to http://stackoverflow.com/questions/6009603/parsing-json-using-system-web-script-seriarilization-or-using-json-net/6010001#6010001
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
Rewrite the sdata class as follows. | |
*/ | |
public class sdata | |
{ | |
public string id { get; set; } | |
public sfrom from { get; set; } | |
public string message { get; set; } | |
public string updated_time { get; set; } | |
public likedatacollection likes { get; set; } | |
} | |
/* | |
add the likedatacollection class | |
*/ | |
public class likedatacollection | |
{ public likedatacollection (){data = new List<likesdata>(); } | |
public List<likesdata> data { get; set; } | |
} | |
/* | |
The other classes remain as they are | |
*/ | |
public class Allstatus | |
{ | |
public List<sdata> data { get; set; } | |
//public scpaging paging { get; set; } | |
} | |
public class likesdata | |
{ public likesdata(){id=name=":|:";}} | |
public string id { get; set; } | |
public string name { get; set; } | |
} | |
public class sfrom | |
{ | |
public string name { get; set; } | |
public string category { get; set; } | |
public string id { get; set; } | |
} | |
public class Request | |
{ | |
public string requestFBData(string action) | |
{ | |
HttpWebRequest req = (HttpWebRequest)WebRequest.Create(action); | |
req.Timeout = 60000; | |
HttpWebResponse resp = (HttpWebResponse)req.GetResponse(); | |
StreamReader sr = new StreamReader(resp.GetResponseStream()); | |
string results = sr.ReadToEnd(); | |
sr.Close(); | |
return results; | |
} | |
} | |
var result = new Request().requestFBData("https://graph.facebook.com/platform/statuses?access_token=" + "202929493078068|886e4149cac7be0fc12e2d4b.0-100002424609963|a9CMw51PYEW3kj4xw77JyEJoRrI"); | |
var deserializedAllStatus = javaScriptSerializer.Deserialize<Allstatus>(result); | |
foreach (sdata sd in deserializedAllStatus.data) | |
foreach (likesdata ld in sd.likes.data) | |
Console.WriteLine(ld.id + ld.name); |
the key is dummy.. i will delete it anyway soon. The returned info is from https://graph.facebook.com/platform/statuses?access_token=< access_token here > This brings a nullreference on the likesdata. I don't know every thing from code looks okay.
I have tried both using system.web.script.serialization and using JSON.net. Both bring null reference.. After initializing the likesdata using contructors.
Adding a constructor in the sdata.. solved it..
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Could you also show what the variable result contains? What is the actual json returned by the server? Please replace any sensitive/personal info with dummy info.