Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

Fetching values from JSON into java variables

Posted on 2014-03-14
2
Medium Priority
?
280 Views
Last Modified: 2014-03-23
HI,
I have the following Json in a String Object in java:
{
    "resCode": 200,
    "resMessage": "Success",
    "interactionId": "65aaeec4-5f3a-45c8-b842-cd27177402b3",
    "interationType": "FlightSearch",
    "sessionId": "83c172f7-a379-4737-926d-0f016be4c59c",
    "response": {
        "request": {
            "tripType": "ONEWAY",
            "tripList": [
                {
                    "origin": "DEL",
                    "destination": "BOM",
                    "departureDate": "2014-03-26"
                }
            ],
            "noOfAdults": 1,
            "noOfChildren": 0,
            "noOfInfants": 0,
            "travelClass": "Economy",
            "showOnlyRefundableFlights": false,
            "showOnlyNonStopFlights": false,
            "hourRanges": null,
            "domain": null
        },
        "searchResults": {
		...
		

Open in new window

           I need to take out the value of response node... from which i need to pick out each of tripType , origin, destination, departureDate,....
            into local variables.
            
            How do i do it ?

Thanks
0
Comment
Question by:Rohit Bajaj
[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 Comments
 
LVL 20

Accepted Solution

by:
chaitu chaitu earned 2000 total points
ID: 39929005
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import org.json.simple.JSONArray;
import org.json.simple.JSONObject;
import org.json.simple.parser.JSONParser;
import org.json.simple.parser.ParseException;
 
public class JsonSimpleExample {
     public static void main(String[] args) {
 
	JSONParser parser = new JSONParser();
 
	try {
 
		Object obj = parser.parse(new FileReader("test.json"));
 
		JSONObject jsonObject = (JSONObject) obj;
 
		JSONObject response = (JSONObject) jsonObject.get("response");
		JSONObject request = (JSONObject) response.get("request");
		String type= (String)request.get("tripType");
		JSONArray tripList= (JSONArray)request.get("tripList");
		JSONObject tripListDetails = (JSONObject)tripList.get(0);
		String origin = (String) tripListDetails.get("origin");
		String departureDate = (String) tripListDetails.get("departureDate");
		System.out.println(departureDate);
 
	} catch (FileNotFoundException e) {
		e.printStackTrace();
	} catch (IOException e) {
		e.printStackTrace();
	} catch (ParseException e) {
		e.printStackTrace();
	}
 
     }
 
}

Open in new window


test.json
******
{

    "response": {
        "request": {
            "tripType": "ONEWAY",
            "tripList": [
                {
                    "origin": "DEL",
                    "destination": "BOM",
                    "departureDate": "2014-03-26"
                }
            ],
            "noOfAdults": 1,
            "noOfChildren": 0,
            "noOfInfants": 0,
            "travelClass": "Economy",
            "showOnlyRefundableFlights": false,
            "showOnlyNonStopFlights": false,
            "hourRanges": null,
            "domain": null
        }
      }
  }
		

Open in new window

0
 
LVL 86

Expert Comment

by:CEHJ
ID: 39929625
Your  json actually defines a list of trips. What are you going to do about that?
0

Featured Post

Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

Question has a verified solution.

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

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 …
In this post we will learn how to connect and configure Android Device (Smartphone etc.) with Android Studio. After that we will run a simple Hello World Program.
The viewer will learn how to implement Singleton Design Pattern in Java.
The goal of the video will be to teach the user the difference and consequence of passing data by value vs passing data by reference in C++. An example of passing data by value as well as an example of passing data by reference will be be given. Bot…
Suggested Courses

604 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