Solved

Java JSON parsing data

Posted on 2013-05-10
3
274 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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This article is meant to give a basic understanding of how to use R Sweave as a way to merge LaTeX and R code seamlessly into one presentable document.
Java functions are among the best things for programmers to work with as Java sites can be very easy to read and prepare. Java especially simplifies many processes in the coding industry as it helps integrate many forms of technology and different d…
Viewers will learn how to properly install Eclipse with the necessary JDK, and will take a look at an introductory Java program. Download Eclipse installation zip file: Extract files from zip file: Download and install JDK 8: Open Eclipse and …
The viewer will be introduced to the technique of using vectors in C++. The video will cover how to define a vector, store values in the vector and retrieve data from the values stored in the vector.

911 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

16 Experts available now in Live!

Get 1:1 Help Now