Solved

Fetching values from JSON into java variables

Posted on 2014-03-14
2
244 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
2 Comments
 
LVL 20

Accepted Solution

by:
chaitu chaitu earned 500 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

Better Security Awareness With Threat Intelligence

See how one of the leading financial services organizations uses Recorded Future as part of a holistic threat intelligence program to promote security awareness and proactively and efficiently identify threats.

Join & Write a Comment

Go is an acronym of golang, is a programming language developed Google in 2007. Go is a new language that is mostly in the C family, with significant input from Pascal/Modula/Oberon family. Hence Go arisen as low-level language with fast compilation…
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…
The goal of this video is to provide viewers with basic examples to understand and use conditional statements in the C programming language.
The viewer will learn how to use the return statement in functions in C++. The video will also teach the user how to pass data to a function and have the function return data back for further processing.

760 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

Need Help in Real-Time?

Connect with top rated Experts

18 Experts available now in Live!

Get 1:1 Help Now