Skip to main content

JSON parsing in JAVA

JSON parsing in JAVA

package jsonsample;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

public class JsonSample {
public static void main(String[] args) {
String str = "{'status': 'ok','found': true,"
+ "'header': {'queried_sn': 'T0650GW872832', 'mfpro_env': 'deere_staging', 'elapsed_seconds': 2.314,'timestamp': '2012-06-14 07:12:18'},"
+ "'machine': { 'mfpro_machine': {'updated_at': '2012-05-22T10:23:54-04:00','matches': 3,'hours': 6809,'id': 1140530,'features': "
+ "[{'key': 'pat','label': 'Power-Angle-Tilt','feature_key': 'cons_blades'}]},"
+ "'make': {'name': 'John Deere','id': 100,'pi': null},"
+ "'mdp': 77092.5,'cat': {'mfpro_label': 'Crawler Dozers','mfpro_key': 'bk:crawler_dozers','pi': 54198},"
+ "'model': {'name': '650G','pi': null},"
+ "'potential_features': [{'key': 'attachments','label': 'Attachments',"
+ "'values': [{'key': 'bar-saw','label': 'Bar Saw'}, ]}],'base_code': '5541T'}}";
try {
JSONObject myjson = new JSONObject(str);
String status = myjson.getString("status").toString();
System.out.println("status===>" + status);
boolean found = myjson.getBoolean("found");
System.out.println("found===>" + found);
String header = myjson.getJSONObject("header").toString();
System.out.println("header===>" + header);
String machine = myjson.getJSONObject("machine").toString();
JSONObject machineJsonObject = new JSONObject(machine);
System.out.println("machine===>" + machine);
String mfpro_machine = machineJsonObject.getJSONObject("mfpro_machine").toString();
System.out.println("jsonArray===>" + mfpro_machine);
JSONObject mfpro_machineJsonObject = new JSONObject(mfpro_machine);
JSONArray featureArray = mfpro_machineJsonObject.getJSONArray("features");
int size = featureArray.length();
System.out.println("size===>" + size + "==" + featureArray);
JSONObject featureObject = null;
for (int i = 0; i < size; i++) {
featureObject = featureArray.getJSONObject(i);
}
System.out.println("featureObject==" + featureObject.toString());
System.out.println("machine===>" + featureObject.getString("key"));

System.out.println("*****************************************************");

String str1 = "{'profiles': [{'name':'john', 'age': 44}, {'name':'Alex','age':11}]}";
JSONObject myjson1 = new JSONObject(str1);
JSONArray the_json_array = myjson1.getJSONArray("profiles");
JSONObject json;

int size1 = the_json_array.length();
for (int i = 0; i < size1; i++) {
String vpn = the_json_array.getJSONObject(i).toString();
json = new JSONObject(vpn);
System.out.println("Array " + i + "========>" + vpn);
System.out.println("name===>" + json.getString("name"));
System.out.println("age===>" + json.getInt("age"));
}

} catch (JSONException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}



Above code is the sample code to parse JSON(Javascript Object Notation) object into java. I have used the sample JSON response which is returned from the webservice and parsed in java.
Download the json-org.jar from the given link and add to your workspace and now run the code. Check the output int the console window.
http://www.docjar.com/jar/json-org.jar

Comments

Popular posts from this blog

Download Report in Excel in JSP

Report in Excel Format in JSP FLIGHT CODE FROM STA ETA Notes T3 4264 ISLE OF MAN 11:40 11:42 LANDED AT 11:43 BA 4081 PARIS-CDG 11:45 11:57 LANDED AT 11:58 BE 843 BELFAST CITY 11:45 11:40 LANDED AT 11:41 Call the above jsp by using any of the click functionality from another .jsp

Basic Java Interview Questions

1. Why java does not support multiple inheritance ? Answer :- To say why java doesn't support inheritance directly like c++ should be the diamond problem faced by c++,(Diamond problem is an ambiguity that arises when two classes B and C inherit from A, and class D inherits from both B and C. If a method in D calls a method defined in A (and does not override it), and B and C have overridden this method differently, then via which class does it inherit: B, or C?) 2. JVM crash is an Error or Exception.? Answer:- JVM crash is an Error as a normal developer as we cannot handle it, but its exception for JVM developer as they can fix it. 3. Print “Hello World!” Without Main Method in Java? Answer:- public class Testing {    static    {        System.out.println("Hello World");    }    public static void main(String[] args)    {    } } ...

Collections in Java

Collections in Java Introduction to Collections Framework The Collections Framework provides a well defined set of interfaces and classes for storing and manipulating the groups of data into a single unit. Introduction to Collections Framework Collections Framework: The Collections Framework provides a well-designed set of interfaces and classes for storing and manipulating the groups of data into a single unit. The collections framework is a unified architecture which is used to represent and manipulate collections. The framework allows the collections to get manipulated independently, additionally it reduces the programming efforts and increases performance. It includes implementation of interfaces and algorithms. Basically it is a unified architecture that consists the following collections: Interfaces: These are the abstract data types that represent collections. With the help of interfaces we manipulate collections independently. A hierarchy is generally formed ...