• 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
  • Paul Clapham
  • Ron McLeod
  • Tim Cooke
  • Junilu Lacar
Sheriffs:
  • Rob Spoor
  • Devaka Cooray
  • Jeanne Boyarsky
Saloon Keepers:
  • Jesse Silverman
  • Stephan van Hulst
  • Tim Moores
  • Carey Brown
  • Tim Holloway
Bartenders:
  • Jj Roberts
  • Al Hobbs
  • Piet Souris

Parsing a JSON string

 
Ranch Hand
Posts: 76
IntelliJ IDE Spring BSD
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
After digging around on le internet, trying out various libraries (Jackson, GSON, json-lib, json-simple), tutorials, and stackoverflow, along with coderanch, I'm at a loss as to how this can be done, and why it's so difficult (to me at least).

What am I trying to do?

I'm attempting to parse a string formatted as JSON. Nothing crazy complex, jsut this:



Now my thinking would be to take that string, parse it through a JSON library that you'd add to your project, and loop through the returned object (or array, either or) and display its' contents. Seemed simple. But I can't get any of the code or tutorials i've found to work at all. Unknown types, constructors that dont' take arguments even though every tutorial says they do, deprecated methods, and classes that in sample code are supposed to exist with libraries but don't.

So I'm asking....how the heck do you do something that in other languages is basically throw a string into a method and handle the results? It can't be as hard as I'm experiencing....right?
 
Saloon Keeper
Posts: 13369
295
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Never had much problems with it myself. I have some experience with Jackson and GSON. Can you show what you have tried so far, and what problems you're experiencing?
 
Sheriff
Posts: 67595
173
Mac Mac OS X IntelliJ IDE jQuery TypeScript Java iOS
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You're going to have trouble converting that JSON because your key values of 0 and 2 aren't legal Java identifiers. So you'll never be able to use a bean as the deserialization, which is the most common and easiest approach.

You may be able to deserialize it to a Map with Integer values as the keys, but that's not as straightforward. As I recall, for Gson, you need to build a type descriptor and pass it in to the deserialization method.

I guess I'd have to ask: why is your JSON formatted with numeric keys? That's odd and nothing I've ever had to deal with in many years of using JSON.
 
Chris Creed
Ranch Hand
Posts: 76
IntelliJ IDE Spring BSD
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Stephan van Hulst wrote:Never had much problems with it myself. I have some experience with Jackson and GSON. Can you show what you have tried so far, and what problems you're experiencing?



Well Finally got some time to give some more kicks at the can and after some more wall bashing, finally got something to work, albeit very simple. JSON simple out of everything ended up working to at least take a string of parse it as JSON. Here's what I have so far.



Basically sample.txt is a file filled with random data I created. Everyline has a random string such as {"foo":"bar"} (the example I had I was still treating JSON like a PHP array...old habits die hard I guess).

Running into some odds issues like it looping if I have multiple JSON enteries on one line (eg {"fooA":"barA","fooB":"barB","fooC":"barC","fooD":"barD"}) and it puking on evey new line character, but I'm chalking it more to my ignorance of java then the parser itself.
 
You showed up just in time for the waffles! And this tiny ad:
Building a Better World in your Backyard by Paul Wheaton and Shawn Klassen-Koop
https://coderanch.com/wiki/718759/books/Building-World-Backyard-Paul-Wheaton
reply
    Bookmark Topic Watch Topic
  • New Topic