Solved

Java JSON parsing data

Posted on 2013-05-10
3
281 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Whether you’re a college noob or a soon-to-be pro, these tips are sure to help you in your journey to becoming a programming ninja and stand out from the crowd.
This article demonstrates how to create a simple responsive confirmation dialog with Ok and Cancel buttons using HTML, CSS, jQuery and Promises
This video teaches viewers about errors in exception handling.
This tutorial explains how to use the VisualVM tool for the Java platform application. This video goes into detail on the Threads, Sampler, and Profiler tabs.

735 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