Solved

Java JSON parsing data

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

Ransomware: The New Cyber Threat & How to Stop It

This infographic explains ransomware, type of malware that blocks access to your files or your systems and holds them hostage until a ransom is paid. It also examines the different types of ransomware and explains what you can do to thwart this sinister online threat.  

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
by zero exception 10 41
Google Maps API and PHP 25 34
jquery datepciker end date add 6 days 6 40
sql import cannot be resolved jsp 3 25
This article will show, step by step, how to integrate R code into a R Sweave document
This article discusses how to create an extensible mechanism for linked drop downs.
The viewer will learn how to implement Singleton Design Pattern in Java.
This theoretical tutorial explains exceptions, reasons for exceptions, different categories of exception and exception hierarchy.

831 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