I finally got this to work using the javascriptserializer to deserialize the json content. Now I have the objects in a dictionary and would like to access the contents to get the key values. I want to grab the values of certain fields only.
{
"data": [
{
"id": "56381779049_10150352298839050",
"from": {
"name": "Paulina Soto",
"id": "1619854594"
},
"to": {
"data": [
{
"name": "Pepsi",
"category": "Food/beverages",
"id": "56381779049"
}
]
},
"message": "La coca es mejor que la pepsi :D.",
"type": "status",
"created_time": "2011-08-11T20:43:18+0000",
"updated_time": "2011-08-11T20:43:18+0000",
"comments": {
"count": 0
}
},
{
"id": "56381779049_10150352296084050",
"from": {
"name": "William Scott Jennings",
"id": "1125852789"
},
"to": {
"data": [
{
"name": "Pepsi",
"category": "Food/beverages",
"id": "56381779049"
}
]
},
"message": "Don't buy the new Pepsi can coming out with pictures of the Empire State building and The Pledge of Allegiance on them. Pepsi left out two little words on the pledge, \"Under God.\" Pepsi said they didn't want to offend anyone. So if we don't buy them they won't be offended when they don't receive our money that has the words \"In God We Trust\" on it. How fast can you re post this? It is offensive to leave out Under God\r\nDon't buy the new Pepsi can coming out with pictures of the Empire State building and The Pledge of Allegiance on them. Pepsi left out two little words on the pledge, \"Under God.\" Pepsi said they didn't want to offend anyone. So if we don't buy them they won't be offended when they don't receive our money that has the words \"In God We Trust\" on it. How fast can you re post this? It is offensive to leave out Under God\r\n",
"type": "status",
"created_time": "2011-08-11T20:39:59+0000",
"updated_time": "2011-08-11T20:39:59+0000",
"comments": {
"count": 0
}
},
{
"id": "56381779049_10150352295939050",
"from": {
"name": "William Scott Jennings",
"id": "1125852789"
},
"to": {
"data": [
{
"name": "Pepsi",
"category": "Food/beverages",
"id": "56381779049"
}
]
},
"message": "Don't buy the new Pepsi can coming out with pictures of the Empire State building and The Pledge of Allegiance on them. Pepsi left out two little words on the pledge, \"Under God.\" Pepsi said they didn't want to offend anyone. So if we don't buy them they won't be offended when they don't receive our money that has the words \"In
code to deserialize;
dim dserial as new javascriptSerializer()
Dim ddict as Dictionary(of String, Object) = dserial.Deserialize(Of Dictionary(Of string, object))(jsonData)
how do I now get the id, from, name, id, to …etc fields?
Any ideas please.
{updated}
appreciate the response. While I was waiting I tried something else and found what seems to be an easier method if I can only get some notes or examples on this.
dim results as list(of JToken) = jobj.("data").Children().ToList()
for each item as Jtoken in results
uid = item("id")
if item.type = JTokenType.Object then
author = item.SelectToken("from.name")
authorId = item.SelectToken("from.id")
end if
msg = item("message")
next
Ok this seems much easier but I do not know how to traverse the token object. The message field is not read because the type is still Object I guess and I was wondering how can I go through this individual JToken and grab the fields.
Deserialization creates the specified object based on the data. So, with what you have you could use code like the following (.Net 4.0 only, and this is C# since I don’t know VB):
and so on.
If you don’t have the dynamic keyword you will have to use reflection, and that’s a long answer.
However, it would be easier if you cast it to the original object type that you used to create that JSON. Or, if this is coming from an external source, create new types in your source code that map to that data tree, and deserialize to that.
Then deserialize to (VB):