Results 1 to 3 of 3

Thread: Json.net parsing

  1. #1

    Thread Starter
    Hyperactive Member
    Join Date
    Nov 2008
    Location
    USA
    Posts
    257

    Json.net parsing

    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:

    Code:
    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

    Code:
    catch exception..
      dim jobj2 as JObject = JObject.parse(responseData)
      description = jobj2("description").tostring
    JSON responsedata:

    Code:
    {
       "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.
    -- Please rate me if I am helpful --

  2. #2
    Frenzied Member dolot's Avatar
    Join Date
    Nov 2007
    Location
    Ancient City, U.S.
    Posts
    1,254

    Re: Json.net parsing


    I couldn't possibly tell you what's going on json wise - perhaps newtonsoft has a forum of their own for these questions? I work with a third party package made by esri, and they have their own forum for asking and answering questions specific to their product.

    But as for your code, rather than doing a Try...Catch, perhaps you could do something like this:
    Code:
    Dim jobj as JObject = JObject.Parse(responseData)
    if jobj isnot nothing then
        'do something
    else
        'do something else
    end if
    I always add to the reputation of those whose posts are helpful, and even occasionally to those whose posts aren't helpful but who obviously put forth a valiant effort. That is, when the system will allow it.
    My war with a browser-redirect trojan

  3. #3

    Thread Starter
    Hyperactive Member
    Join Date
    Nov 2008
    Location
    USA
    Posts
    257

    Re: Json.net parsing

    Thank you for your response.
    -- Please rate me if I am helpful --

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width