Solved

Java JSON parsing data

Posted on 2013-05-10
3
272 Views
Last Modified: 2013-10-02
Hello,  I am using Java with GSON. I am having an issue returning values in a organized format.  I want a simple java app to handle parsing data to my classes. When I return values using System.pringln(data2); I seem to obtain unexpected null value for NAME. What do you think the issue is? Why a null value?
<META NAME="ROBOTS" CONTENT="NOINDEX, NOFOLLOW">

Open in new window

<META NAME="ROBOTS" CONTENT="NOINDEX, NOFOLLOW">

Open in new window

import com.google.gson.Gson;

public class ReadJson {

    public static void main(String... args) throws Exception {

	String JSON_STRING = "{\"data\":[{\"NAME\":\"Brandy\",\"LOCATION\":\"Redding, CA\"},{\"NAME\":\"Jacob\",\"LOCATION\":\"Redding, CA\"},{\"NAME\":\"Tatiana\",\"LOCATION\":\"Wonderland\"},{\"NAME\":\"Tedo\",\"LOCATION\":\"Cottonwood\"}]}";

    DataJSON data2 = new Gson().fromJson(JSON_STRING, DataJSON.class);
    System.out.println(data2);
	
    }
}
class DataJSON {
    public String NAME;
    public String LOCATION;
    public String getName() { return NAME; }
    public String getLocation() { return LOCATION;}

    public void setName(String NAME) { this.NAME = NAME; }
    public void setLoction(String LOCATION) { this.LOCATION = LOCATION; }
    public String toString() {
    	return String.format("NAME:%s,LOCATION:%s", NAME, LOCATION);
    }
}

Open in new window

0
Comment
Question by:bcamacho
  • 2
3 Comments
 
LVL 82

Expert Comment

by:leakim971
ID: 39157286
Your JSON string contain an array/list not a simple DataJSON object.
Use :
import com.google.gson.Gson;

public class ReadJson {

    public static void main(String... args) throws Exception {
		String JSON_STRING = "{\"data\":[{\"NAME\":\"Brandy\",\"LOCATION\":\"Redding, CA\"},{\"NAME\":\"Jacob\",\"LOCATION\":\"Redding, CA\"},{\"NAME\":\"Tatiana\",\"LOCATION\":\"Wonderland\"},{\"NAME\":\"Tedo\",\"LOCATION\":\"Cottonwood\"}]}";
	
	    Data data2 = new Gson().fromJson(JSON_STRING, Data.class);
	    System.out.println(data2.getDataJSON().get(0).toString());
    }
}

Open in new window

And :
import java.util.List;

public class Data {
    private List<DataJSON> data;
    public List<DataJSON> getDataJSON() { return data; }
    public void setDataJSON(List<DataJSON> data) { this.data = data; }
}

Open in new window

And :
public class DataJSON {
    public String NAME;
    public String LOCATION;
    public String getName() { return NAME; }
    public String getLocation() { return LOCATION;}

    public void setName(String NAME) { this.NAME = NAME; }
    public void setLocation(String LOCATION) { this.LOCATION = LOCATION; }
    public String toString() {
    	return String.format("NAME:%s,LOCATION:%s", NAME, LOCATION);
    }
}

Open in new window

0
 
LVL 1

Accepted Solution

by:
bcamacho earned 0 total points
ID: 39528667
I waited for a solution and honestly time passed by where I found a different solution. Please close this question Expert-Exchange!
0
 
LVL 1

Author Closing Comment

by:bcamacho
ID: 39539249
To long of time has passed from the point I originally ask the question, with a different solution in-place for parsing data there is no need to have this question open.
0

Featured Post

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

If you haven’t already, I encourage you to read the first article (http://www.experts-exchange.com/articles/18680/An-Introduction-to-R-Programming-and-R-Studio.html) in my series to gain a basic foundation of R and R Studio.  You will also find the …
This article discusses how to create an extensible mechanism for linked drop downs.
The viewer will learn how to pass data into a function in C++. This is one step further in using functions. Instead of only printing text onto the console, the function will be able to perform calculations with argumentents given by the user.
The viewer will learn the basics of jQuery including how to code hide show and toggles. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery…

757 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

21 Experts available now in Live!

Get 1:1 Help Now