• Post Reply Bookmark Topic Watch Topic
  • New Topic

Parsing a JSON string  RSS feed

 
Chris Creed
Ranch Hand
Posts: 71
BSD IntelliJ IDE Spring
  • Mark post as helpful
  • send pies
  • 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?
 
Stephan van Hulst
Saloon Keeper
Posts: 7973
143
  • Mark post as helpful
  • send pies
  • 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?
 
Bear Bibeault
Author and ninkuma
Marshal
Posts: 66306
152
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • 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: 71
BSD IntelliJ IDE Spring
  • Mark post as helpful
  • send pies
  • 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.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!