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

Uploading data on Remote Server in Java

This blog demonstrate how to upload data from file to remote server/database. Follow the steps as shown. 1. Create new Dynamic Web Project. 2. Now create all the packages as show in the image below. 3. Create controller with name ImportContact.java 4. Create pojo with name Contact.java 5. Create database layer code with name ImportContactBroker.java 6. Create connectivity with name DBConnection.java 7. Create db connectivity setting file as connectionSetting.java 8. Create jsp page with importcontact.jsp 9. Create supporting files like Constant.java and CsvReader.java 10. You can download the war file and import in your workspace. 11. MultipartRequest in Servlet and inside jsp helps to upload file on the remote server. 12. Download sample csv file from here . 13. Create Table with name Contact in Database as given in configuration file. 14.Just compile and run the code. ImportContact.java package com.uploadfile.controller; import java.io.BufferedReader; impo...

JOINs in MySQL

JOIN's is used to query data from two or more than two table based on relationship between certain columns in these tables. Tables in Database are related to each other with some constraints like (Primary Key & Foreign Key) Primary Key :- Column with unique value for each row that is not value matching to each other in the column. Foreign Key :- Two tables are mapped through concept of primary and foreign key, that means primary key of one table is used in other table as foreign key. fig : Primary Key and Foreign Key Example Query to create the above two tables. CREATE TABLE `project_mst` (   `PK_PROJECT_ID` varchar(10) NOT NULL,   `PROJECT_NAME` varchar(100) default NULL,   `PROJECT_DURATION` varchar(50) default NULL,   `FK_MANAGER_ID` varchar(10) default NULL,   PRIMARY KEY  (`PK_PROJECT_ID`) ) CREATE TABLE `manager_mst` (   `PK_MANAGER_ID` varchar(10) NOT NULL,   `MANAGER_...

Creating Pdf in multiple Languages in Java

Creating Pdf in multiple Languages in Java This code demonstrate how to create pdf in multiple languages in java by using ITEXT pdf creator. To use this sample code download itext.jar To display different locale on pdf you need to download font known as Arialuni.ttf and added to the project Most important thing is the encoding part set encoding to "Identity-H" (String encoding = "Identity-H";) as well as create font for the same to take effect in pdf create font (Font fontNormal = FontFactory.getFont(("c:/windows/fonts/arialuni.ttf"), encoding,BaseFont.EMBEDDED, 8, Font.NORMAL);) You can download the source code from here import java.io.FileOutputStream; import com.lowagie.text.Chunk; import com.lowagie.text.Document; import com.lowagie.text.Font; import com.lowagie.text.FontFactory; import com.lowagie.text.HeaderFooter; import com.lowagie.text.Paragraph; import com.lowagie.text.Phrase; import com.lowagie.text.pdf....