• 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

Extracting data from a JSON file/URL/Object/String in Java

 
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi there,

I hope someone can help me out as I am really struggling for this.

Basically what I am looking for is to be able to extract data properly from a json file or string or object or anything else in that format.

I found many tutorials/guide but nothing that could help me really.

I can read a json url but once I have the content I want to properly separate it so I can display it in a more readable format.

How do I do this in java? and what is the best parser to do this?

Thank you
 
Ranch Hand
Posts: 608
Firefox Browser Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Jackson is the usual JSON parser used in java - http://jackson.codehaus.org/
They got a tutorial and stuff on how to use it.
 
Daniel Ross
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thank you for the reply.

If I put the code in the following way then it gives me an error on the first line


it doesn't give my any error if I put it in this way




Why so?
ps: @Bear sorry about it, I amended the name now.
 
Rancher
Posts: 600
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Daniel:

A few things:

1. Java classes start with capitals, so the class should be ReadJson, not readJson.
2. You can't have code outside of a method (hint: see line 10 of your erroring class)
3. Classes don't throw exceptions, methods do.

John.
 
Saloon Keeper
Posts: 15630
366
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Well, class names should be nouns, so the name should really be JsonReader.
 
Daniel Ross
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi yes I realized it and already change it .

Thanks for the info

I manage to separate the json file, so now I can show each token separately.
What I am trying to do is to to save the number of users and their details from the json string
Here is the user class I made



This class has all the variables, the string I want to get the data from has details such as when the file was created and all the users in that file.
I want to store the data in a way that it shows :
Completed in: times
query: the search query used
Number of users: integer
name:
user id:
geo:
etc.....
and then show the user two details
show the user 3 details etc....

How do I save it so that users are saved in an array calling the User class I have created.

Here is the code I am trying to make but I don't know how to continue:




[Edit]Hope you haven’t used line numbers; your comment was so long and difficult to read, that I divided it into 5 lines. CR[/edit]
 
Daniel Ross
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
hey

This is the code I am using to save it in an array but with no success

First I made an instance of the UsedID class in an array


I, then stored the value in by calling that class in the same way as I done with the other values ,but in this case it's an array.


 
John de Michele
Rancher
Posts: 600
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Daniel:

Another niggly style issue: member variable names should be in Camel Case, starting with a lowercase letter. Underscores between words should be avoided, unless you're doing a static final in all uppercase.

John.
 
Marshal
Posts: 79464
379
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

John de Michele wrote:Daniel:

Another niggly style issue: . . . .

You are also using inconsistent indentation. Don’t use tabs for indenting. Use spaces. Follow the keyword if with a single space. Don’'t try to compress a method into one line. A “getXXX” method returning a boolean should be called “isXXX” or “hasXXX”.
All little things, but inconsistencies make it harder to read your code.
 
roses are red, violets are blue. Some poems rhyme and some are a tiny ad:
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic