Solved

Java JSON parsing data

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

Optimize your web performance

What's in the eBook?
- Full list of reasons for poor performance
- Ultimate measures to speed things up
- Primary web monitoring types
- KPIs you should be monitoring in order to increase your ROI

Question has a verified solution.

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

Introduction This article is the second of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article covers the basic installation and configuration of the test automation tools used by…
When we want to run, execute or repeat a statement multiple times, a loop is necessary. This article covers the two types of loops in Python: the while loop and the for loop.
The goal of the tutorial is to teach the user how to use functions in C++. The video will cover how to define functions, how to call functions and how to create functions prototypes. Microsoft Visual C++ 2010 Express will be used as a text editor an…
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 …

627 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