• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Poorly formatted JSON parsing

 
Ranch Hand
Posts: 87
1
Netbeans IDE Firefox Browser Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I use code above to parse JSON in a try-catch.



I use 2 as index in code below, because I would get values from candidates. I think, that is strange, because there isn't key-value pairs in array. I would like to get only the first key from this array (In this example subtest1.), its value isn't make sense.

 
Saloon Keeper
Posts: 15510
363
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Well, it looks to me like you're accessing the object wrongly.

When I look at the JSON, I see a root object containing an images array which contains only 1 object. This object has time, transaction and candidates entries.

Why are you treating the root object as an array? If you're planning on getting candidates from an image, then why are you treating the image as an array and not a JSON object?

Let me reformat your JSON so it's more clear:
 
Kovacs Akos
Ranch Hand
Posts: 87
1
Netbeans IDE Firefox Browser Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
There is only one image in all the times and only need the identify of the first candidate. So candidates is also an object?
 
Stephan van Hulst
Saloon Keeper
Posts: 15510
363
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
No. "candidates" refers to a JSON array. Each entry in the array is a JSON object, and represents one candidate.

The problem is that the JSON structure you have there doesn't allow you to easily extract the name of a candidate, because it's used as a key and not as a value.

JsonObject implements Map<String, JsonValue> and its entries are in encounter-order, so you can use an iterator to get the first key from the key set.
 
Kovacs Akos
Ranch Hand
Posts: 87
1
Netbeans IDE Firefox Browser Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I see!
Yes, it is a really interesting situation because of these special "key-value" pairs. I haven't found some examples like this.
 
Kovacs Akos
Ranch Hand
Posts: 87
1
Netbeans IDE Firefox Browser Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I have tried to parse with this updated code:



It gives following error for this format:

org.json.JSONException: No value for candidates

 
Rancher
Posts: 4801
50
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
"candidates" is part of the elements in the "images" array, it is not part of the root element of the JSON.

root -> elements[0] -> candidates
 
Stephan van Hulst
Saloon Keeper
Posts: 15510
363
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Dave Tolls wrote:root -> elements[0] -> candidates


I'm assumng Dave meant root -> images[0] -> candidates.

Besides what Dave said, when you're trying to get the candidates array from the first image object, why are you using getString()? The candidates array is a JsonArray, not a String.
 
Kovacs Akos
Ranch Hand
Posts: 87
1
Netbeans IDE Firefox Browser Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
So should I define two JSONarrays, array of array?
 
Dave Tolls
Rancher
Posts: 4801
50
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Oh yes, sorry..."images"...
 
Stephan van Hulst
Saloon Keeper
Posts: 15510
363
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Yes, you should define two arrays, but no, one is not an array of arrays. Look closely at your structure:

root -> image array -> image object -> candidate array -> candidate object

The image array is not an array of arrays. It's an array of image objects. The image object contains an array of candidate objects.
 
Kovacs Akos
Ranch Hand
Posts: 87
1
Netbeans IDE Firefox Browser Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I have tried to reorganize my code:



But no luck, name string is empty.
 
Kovacs Akos
Ranch Hand
Posts: 87
1
Netbeans IDE Firefox Browser Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Sorry, there is a mistake in code above, second jsonarray should be replaced and it gives the value, that belongs to this so called "key" as in example: 0.802138030529022.
 
Dave Tolls
Rancher
Posts: 4801
50
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Debug it.
Log the JSONObject(s) produced.
Also log the value of 'user'.
I suspect this is not doing what you think it's doing.
 
Stephan van Hulst
Saloon Keeper
Posts: 15510
363
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You should give your variables more descriptive names.

Why not call the first JsonArray 'images', the JsonObject you get from it 'image', and the same for 'candidates' and 'candidate'?

Finally, you can get the name using the keys() iterator.
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
reply
    Bookmark Topic Watch Topic
  • New Topic