File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Java in General and the fly likes Extracting data from a JSON file/URL/Object/String in Java Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Soft Skills this week in the Jobs Discussion forum!
JavaRanch » Java Forums » Java » Java in General
Bookmark "Extracting data from a JSON file/URL/Object/String in Java" Watch "Extracting data from a JSON file/URL/Object/String in Java" New topic
Author

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

Daniel Ross
Greenhorn

Joined: Nov 26, 2010
Posts: 10
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
Saifuddin Merchant
Ranch Hand

Joined: Feb 08, 2009
Posts: 606

Jackson is the usual JSON parser used in java - http://jackson.codehaus.org/
They got a tutorial and stuff on how to use it.


Cheers - Sam.
Twisters - The new age Java Quiz || My Blog
Daniel Ross
Greenhorn

Joined: Nov 26, 2010
Posts: 10
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.
John de Michele
Rancher

Joined: Mar 09, 2009
Posts: 600
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.
Stephan van Hulst
Bartender

Joined: Sep 20, 2010
Posts: 3649
    
  17

Well, class names should be nouns, so the name should really be JsonReader.
Daniel Ross
Greenhorn

Joined: Nov 26, 2010
Posts: 10
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

Joined: Nov 26, 2010
Posts: 10
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

Joined: Mar 09, 2009
Posts: 600
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.
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 40052
    
  28
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.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Extracting data from a JSON file/URL/Object/String in Java