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
Solved

Fetching values from JSON into java variables

Posted on 2014-03-14
2
258 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

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering 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

Suggested Solutions

Title # Comments Views Activity
map related example 6 55
MySQL  on Tomcat 8 67
Problem to App 4 97
mysql jsp example issue 32 48
How to remove superseded packages in windows w60 or w61 installation media (.wim) or online system to prevent unnecessary space. w60 means Windows Vista or Windows Server 2008. w61 means Windows 7 or Windows Server 2008 R2. There are various …
Introduction This article is the last of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article covers our test design approach and then goes through a simple test case example, how …
The viewer will learn how to pass data into a function in C++. This is one step further in using functions. Instead of only printing text onto the console, the function will be able to perform calculations with argumentents given by the user.
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.

792 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