?
Solved

Fetching values from JSON into java variables

Posted on 2014-03-14
2
Medium Priority
?
273 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

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Having just graduated from college and entered the workforce, I don’t find myself always using the tools and programs I grew accustomed to over the past four years. However, there is one program I continually find myself reverting back to…R.   So …
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.
The goal of the video will be to teach the user the concept of local variables and scope. An example of a locally defined variable will be given as well as an explanation of what scope is in C++. The local variable and concept of scope will be relat…
This video will show you how to get GIT to work in Eclipse.   It will walk you through how to install the EGit plugin in eclipse and how to checkout an existing repository.
Suggested Courses

765 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