Here I am using a Service from the following URL ,
http://pnrapi.appspot.com/<pnr-number>
for example ,
http://pnrapi.appspot.com/1416780219
This gives the data in the following format ,
{'status': 'OK', 'data': {'passenger': [{'status': 'G3 , 45', 'seat_number': 'W/L 15,CK'}],
'from': 'SC', 'alight': 'YPR', 'pnr_number': '1416780219', 'train_number': '*12735',
'to': 'YPR', 'board': 'SC', 'train_name': 'YPR GARIB RATH',
'travel_date': {'date': '26-6-2011', 'timestamp': 1309046400}, 'class': '3A'}}
you need to have the following jar files in the classpath ,
D:\fmw1114\Middleware\jdk160_21\bin\javaw.exe -client -classpath C:\JDeveloper\mywork\Application5\Project1\classes;D:\commons-httpclient-3.1\commons-io-2.0.1.jar;D:\commons-httpclient-3.1\json-lib-2.4-jdk15.jar;D:\commons-httpclient-3.1\commons-httpclient-3.1.jar;D:\commons-httpclient-3.1\commons-lang-2.6.jar;D:\commons-httpclient-3.1\commons-logging-1.1.1.jar;D:\commons-httpclient-3.1\commons-logging-adapters-1.1.1.jar;D:\commons-httpclient-3.1\commons-logging-api-1.1.1.jar;D:\commons-httpclient-3.1\commons-logging-tests.jar;D:\commons-httpclient-3.1\ezmorph-1.0.6.jar;D:\commons-httpclient-3.1\commons-collections-3.2.1.jar;D:\commons-httpclient-3.1\commons-beanutils-1.8.3.jar;D:\commons-httpclient-3.1\commons-beanutils-bean-collections-1.8.3.jar;D:\commons-httpclient-3.1\commons-beanutils-core-1.8.3.jar project1.ParsePNR
package project1;
import java.io.BufferedReader;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.URL;
import net.sf.json.JSONArray;
import net.sf.json.JSONObject;
import net.sf.json.JSONSerializer;
import org.apache.commons.io.IOUtils;
public class ParsePNR {
public ParsePNR() {
super();
}
public static void main(String[] args) throws Exception {
// InputStream is =
// ParsingJSON.class.getResourceAsStream( "c:\\sample-json.txt");
// String jsonTxt = IOUtils.toString( is );
/*
String test = "\"status\" : \"OK\",\n" +
"\"data\" : { \n" +
" \"train_name\" : \"ERNAKULAM EXP\",\n" +
" \"train_number\" : \"*12677\",\n" +
" \"from\" : \"SBC\",\n" +
" \"to\" : \"CBE\",\n" +
" \"alight\" : \"CBE\",\n" +
" \"board\" : \"SBC\",\n" +
" \"class\" : \"2S\",\n" +
" \"travel_date\" : \"2- 6-2011\"\n" +
" \"passenger\" : [ { \"seat_number\" : \"D8 , 31,GN\", \"status\" : \"CNF\" },\n" +
" { \"seat_number\" : \"D8 , 32,GN\", \"status\" : \"CNF\" } ]\n" +
" }";
// Sample Data (1 passenger)
{'status': 'OK', 'data': {'passenger': [{'status': 'G3 , 45', 'seat_number': 'W/L 15,CK'}], 'from': 'SC', 'alight': 'YPR', 'pnr_number': '1416780219', 'train_number': '*12735', 'to': 'YPR', 'board': 'SC', 'train_name': 'YPR GARIB RATH', 'travel_date': {'date': '26-6-2011', 'timestamp': 1309046400}, 'class': '3A'}}
// Sample Data (2 passenger)
{'status': 'OK', 'data': {'passenger': [{'status': 'G3 , 45', 'seat_number': 'W/L 15,CK'},{'status': 'G3 , 46', 'seat_number': 'W/L 16,CK'}], 'from': 'SC', 'alight': 'YPR', 'pnr_number': '1416780219', 'train_number': '*12735', 'to': 'YPR', 'board': 'SC', 'train_name': 'YPR GARIB RATH', 'travel_date': {'date': '26-6-2011', 'timestamp': 1309046400}, 'class': '3A'}}
Replace the class with class1 as class is a reserved word in java.
*/
String pnr="";
if (args.length == 0 )
pnr = "1416780219";
else
pnr = args[0];
System.setProperty("http.proxyHost", "www-proxy.us.oracle.com");
System.setProperty("http.proxyPort", "80");
System.setProperty("http.nonProxyHosts","*.us.oracle.com|*.oraclecorp.com|*.idc.oracle.com|*.uk.oracle.com|localhost");
URL url = new URL("http://pnrapi.appspot.com/"+pnr);
// Read all the text returned by the server
BufferedReader in = new BufferedReader(new InputStreamReader(url.openStream()));
String str="";
String line;
while ((line = in.readLine()) != null) {
// str is one line of text; readLine() strips the newline character(s)
str= str.concat(line );
// System.out.println(line);
System.out.println(str);
}
in.close();
str= str.replace("'class':","'class1':");
/*String jsonTxt ="{'status': 'OK', 'data': {'passenger': [{'status': 'G3 , 45', 'seat_number': 'W/L 15,CK'},{'status': 'G3 , 46', 'seat_number': 'W/L 16,CK'}], " +
"'from': 'SC', 'alight': 'YPR', 'pnr_number': '1416780219', " +
"'train_number': '*12735', 'to': 'YPR', 'board': 'SC', " +
"'train_name': 'YPR GARIB RATH', 'travel_date': {'date': '26-6-2011', 'timestamp': 1309046400}, " +
"'class1': '3A'}}"; */
String jsonTxt = str;
JSONObject json = (JSONObject) JSONSerializer.toJSON( jsonTxt );
String status = json.getString( "status" );
if (status.compareToIgnoreCase("INVALID") == 0) {
System.out.println("Status is "+status);
System.exit(0);
}
JSONObject data = json.getJSONObject("data");
String train_name = data.getString("train_name");
String train_number = data.getString("train_number");
String class_d = data.getString("class1");
String from = data.getString("from");
String alight = data.getString("alight");
JSONObject travel_date = data.getJSONObject("travel_date");
String date = travel_date.getString("date");
System.out.printf( "status: %s\n", status );
System.out.printf( "Train number : %s Tain Name : %s \n", train_number, train_name );
System.out.printf( "From : %s To : %s Date Of Travel : %s Class : %s\n", from, alight , date , class_d );
JSONArray passenger = data.getJSONArray("passenger");
int i1=0;
for (Object o : passenger) {
i1++;
JSONObject passengerDetails = (JSONObject) o;
String status_p = passengerDetails.getString("status");
String seat_number = passengerDetails.getString("seat_number");
System.out.printf( "Passenger : %d Current Status : %s Previous Staus : %s\n", i1, status_p, seat_number );
}
}
}
3 comments:
sir this link went down three days back can you please suggest something that also returns the result in json script
Hello Sir, Can u explain the full code and how it is working????
is there any better way of do it using any other scripting or server side languages????
pnr data is good thanks
Indian railway pnr status
Post a Comment