• 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
  • Tim Cooke
  • Jeanne Boyarsky
  • Liutauras Vilda
Sheriffs:
  • Frank Carver
  • Henry Wong
  • Ron McLeod
Saloon Keepers:
  • Tim Moores
  • Frits Walraven
  • Tim Holloway
  • Stephan van Hulst
  • Carey Brown
Bartenders:
  • Al Hobbs
  • Piet Souris
  • Himai Minh

How can I access inner JSON elements with simple json in JAVA?

 
Ranch Hand
Posts: 39
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator


I have this JSON for example:



This is the code that I have written in Java:



This prints out the serial number...but what I would like to do is to acces for example in mesaje -> cif , or. mesaje -> data_creare....the library used is json-simple...can you please help me with how I could do that ? Thanks in advance
 
Saloon Keeper
Posts: 14299
321
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Well, you could do:

I'm not a big fan of using weakly typed XML/JSON parsing libraries though. Instead, you might want to consider using a JAXB or JSON-B implementation in your application.

 
Saloon Keeper
Posts: 26037
186
Android Eclipse IDE Tomcat Server Redhat Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Just a note on filename paths in Java.

I recommend this form:


Java will accept forward ("real") slashes in filename paths in Windows and they're safer than backslashes, since you don't have to remember to double them.

Using "->" doesn't work in Java, of course. Java doesn't do pointers like C/C++. But the JSONParser does produce the Java equivalent. Your main problem was that you didn't notice the finer details of the JSON sample you used.

Take a closer look here:


Notice the "[" indicating that the sub-elements are an array. And the "{", indicating that you have a dictionary as the first element in the array. Well, actually, other elements as well, but we're looking for a particular property value here.

So to get the first "cif" value, the standard JavaBean syntax would be jsonObject.get("mesaje")[0].get("cif"], if I'm not mistaken.
 
Stephan van Hulst
Saloon Keeper
Posts: 14299
321
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Tim Holloway wrote:Java will accept forward ("real") slashes in filename paths in Windows and they're safer than backslashes, since you don't have to remember to double them.


You can also dance around the entire issue like this:
 
Tim Holloway
Saloon Keeper
Posts: 26037
186
Android Eclipse IDE Tomcat Server Redhat Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Stephan van Hulst wrote:
You can also dance around the entire issue like this:



You could. But it's a lot more overhead. And it's not like using Paths.get() is making anything more portable as long as one of the arguments is "D:".
 
Come have lunch with me Arthur. Adventure will follow. This tiny ad:
the value of filler advertising in 2021
https://coderanch.com/t/730886/filler-advertising
reply
    Bookmark Topic Watch Topic
  • New Topic