json parsing not working

Feb 16, 2011 at 3:44 PM

I am accessing the facebook api and using the json.net library (newtonsoft.json.net) I declare a Jobject to parse the content and look for the specific elements and get their values. Everything works fine for the first few but then I get this unexplained nullexception error " (Object reference not set to an instance of an object)

Now I took a look at the declaration but cannot see how to change it. Any help appreciated:

 

Dim jobj as JObject = JObject.Parse(responseData)
message = jobj("message").tostring

The error occurs at the last line above.I check to see if message is null and then look for the next desired field as follows

catch exception..
  dim jobj2 as JObject = JObject.parse(responseData)
  description = jobj2("description").tostring

JSOn response:

 

{
   "id": "5281959998_126883980715630",
   "from": {
   "name": "The New York Times",
   "category": "Company",
   "id": "5281959998"
},
  "picture": "http://external.ak.fbcdn.net  /safe_image.php?d=e207958ca7563bff0cdccf9631dfe488&w=
90&h=90&url=http\u00253A\u00252F\u00252Fgraphics8.nytimes.com\u00252Fimages\u00252F2011\u00252F02\u00252F04\u00252Fbusiness\u00252FMadoff\u00252FMadoff-thumbStandard.jpg",
  "link": "http://nyti.ms/hirbn0",
   "name": "JPMorgan Said to Have Doubted Madoff Long Before His Scheme Was Revealed",
   "caption": "nyti.ms",
   "description": "Newly unsealed court documents show that bank
   executives were suspicious of Bernard Madoff\u2019s accounts 
   and steered clients   away from him but did not alert regulators.",
   "icon": "http://static.ak.fbcdn.net/rsrc.php/yD/r/aS8ecmYRys0.gif",
   "type": "link",
   "created_time": "2011-02-04T16:09:03+0000",
   "updated_time": "2011-02-06T20:09:51+0000",
  "likes": {
  "data": [
     {
        "name": "Siege Ind.",
        "category": "Product/service",
        "id": "152646224787462"
     },
     {
        "name": "Lindsey Souter",
        "id": "100000466998283"
     },

This is one example where "message" does not appear in the first few lines but appears later. So what I do is look for position of message and description and which ever is first go and get that and if I get an error or the fields do not return anything, I try and parse by regex and even that is not working right.

I checked for the field returning null and when I look at response data I see the field message / description present but it does not read the value.

Appreciate any help

Feb 16, 2011 at 6:33 PM

I ended up using the jsontextreader and can identify the property name but still cannot read the value.

Feb 22, 2011 at 6:00 PM
Edited Feb 22, 2011 at 6:03 PM

C#

var jo = JObject.Parse(jsonString);

var message = (jo.SelectToken("message") != null && !string.IsNullorEmpty((string)jo.SelectToken("message"))) ? (string)jo.SelectToken("message") : string.Empty;

What's happening is you need to test whether or not that "message" JToken actually exists and if it does, then cast it to string value so it doesn't error out.