Reputation: 3345
I am trying to parse JSON response from a httpwebrequest fetch and a little unsure after reading posts on it, as to what would be the best approach for me. I access the facebook graph api and would like to parse the all fields for a given post.
"data": [
{
"id": "17xxxxxxxxxxxxx_xxxxxxxxxxxxxxxxxxx",
"from": {
"name": "Lxxxxxx",
"category": "Sports league",
"id": "17xxxxxxxxxxxxx"
},
"picture": "http://external.ak.fbcdn.net/safe_image.php?d=AQB4GscSy-2RHY_0&w=130&h=130&url=http\u00253A\u00252F\u00252Fwww.ligabbva.com\u00252Fquiz\u00252Farchivos\u00252Fbenzema-quiz-facebook.png",
"link": "http://www.xxxxxva.com/quiz/index.php?qid=34",
"source": "http://www.lxxxxva.com/modulos/redirectQuiz.php?name=benzema&q=34&time=1312827103",
"name": "DEMUESTRA CU\u00c1NTO SABES SOBRE... BENZEMA",
"caption": "www.xxxxxva.com",
"description": "Demuestra cu\u00e1nto sabes sobre Karim Benzema, delantero del Real Madrid.",
"icon": "http://static.ak.fbcdn.net/rsrc.php/v1/yj/r/v2OnaTyTQZE.gif",
"type": "video",
"created_time": "2011-08-08T18:11:54+0000",
"updated_time": "2011-08-08T18:11:54+0000",
"likes": {
"data": [
{
"name": "Jhona Arancibia",
"id": "100000851276736"
},
{
"name": "Luis To\u00f1o",
"id": "100000735350531"
},
{
"name": "Manuel Raul Guerrero Cumbicos",
"id": "100001485973224"
},
{
"name": "Emmanuel Gutierrez",
"id": "100000995038988"
}
],
"count": 127
},
"comments": {
"count": 33
}
},
{
"id": "17xxxxxxxxxxxxxxxx_xxxxxxxxxxxxx",
"from": {
"name"
not all fields will always be present but I need to be able to iterate through each post.
Update:
I am receiving an error for the following line:
dim serializer as new jsonSerializer()
dim jsArray as JArray = directCast(serializer.Deserialize(New JsonTextReader _
(New StringReader(responseData))), JArray)
ERROR: unable to cast object of type newtonsoft.json.linq.jobject to type newtonsoft.json.ling.jobject
Upvotes: 2
Views: 1932
Reputation: 20620
These references will help you, I believe.
http://dorobantu.me/post/2010/08/22/Deserializing-JSON-to-anonymous-types-in-C.aspx
Upvotes: 1
Reputation: 8278
you could use the newtonsoft JSON deserialiser
you would set up a Data Entity with each of the properties and their sub properties and then use JSON attributes to map the entities to the properties
sample code from a similar thing I was doing:
[JsonObject(MemberSerialization.OptIn)]
public class Data
{
public override string ToString()
{
return Name;
}
[JsonProperty(PropertyName = "id")]
public string Id{ get; set; }
}
public Data DeserialiseData()
{
var data = JsonConvert.DeserializeObject<Data>("JSON string");
return data;
}
more information can be found here
Upvotes: 5