Question

I have to parse JSON. I am using Jackson library to parse it.

My code:

JSONObject root = (JSONObject)parser.parse(response);
JSONArray users = (JSONArray) root.get("response");

Everything would be fine if not a that number in from of elements (1192220) which actually represents a length of a structure. When I am reading it using root.get("response") this number appears to be a first element in users array. I really don't want that. Of course, I can manually truncate an array, but probably it should a better way of doing it. Any suggestions?

{
   "response":[
      1192220,
      {
         "uid":39377403,
         "first_name":"John",
         "last_name":"Smith",
         "screen_name":"Super cool guy",
         "interests":"N/A"
      },
      {
         "uid":19439900,
         "first_name":"Natalie",
         "last_name":"Brook",
         "screen_name":"nutaloveis",
         "interests":"bike"
      },
      {
         "uid":5857176,
         "first_name":"James",
         "last_name":"Mercer",
         "screen_name":"alenkashishkova"
      }, .....]
 }
Was it helpful?

Solution

Filter it out later

If that number allways is the first element of that array, why dont you just skip it when iterating over the array later on (Start your for-loop at index 1 instead of 0)?

If numbers appear all over the array or at random indexes but you only want to get the objects (e.g. filter out all numbers), you could use javas instanceof operator and check whether it is an instance of JSONObject or Integer and then skip it respectivly.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top