How JSON works in JAVA…

package com.webservice;

import java.util.Iterator;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

/**
 *
 * @author Taher_JAVAHUNTER
 */
public class JSONTest {
    
    public static void main(String[] args) throws JSONException {
        JSONTest nc = new JSONTest();
        String json1 = nc.jsonTest_1();
        String json2 = nc.jsonTest_2();
        System.out.println("jsonTest_1 : "+json1);
        System.out.println("jsonTest_2 : "+json2);                                              
        nc.readJSON1(json1);
        nc.readJSON2(json2);
    }
    
    public void readJSON1(String json) throws JSONException{
        JSONArray jsonArray = new JSONArray(json);        
        for (int i = 0; i < jsonArray.length(); i++) {
            JSONObject jSONObject = jsonArray.getJSONObject(i);            
            for (Iterator it = jSONObject.keys(); it.hasNext();) {
                String key = it.next().toString();
                System.out.println("Key_1 : "+key);                    
                System.out.println("Value_1 : "+jSONObject.getString(key));                    
            }            
        }
    }
    
    public void readJSON2(String json) throws JSONException{
        JSONArray jsonArray = new JSONArray(json);        
        for (int i = 0; i < jsonArray.length(); i++) {
            JSONObject jSONObject = jsonArray.getJSONObject(i);            
            for (Iterator it = jSONObject.keys(); it.hasNext();) {
                String key = it.next().toString();
                System.out.println("Key_2 : "+key);                    
                System.out.println("Value_2 : "+jSONObject.getString(key));                    
            }            
        }
    }
    
    public String jsonTest_1() throws JSONException{
        JSONArray jSONArray = new JSONArray();
        JSONObject jSONObject1 = new JSONObject();
        jSONObject1.put("abc", 100);        
        jSONObject1.put("xyz", 200);
        jSONArray.put(0,jSONObject1);        
        JSONObject jSONObject2 = new JSONObject();
        jSONObject2.put("pqr", 1000);
        jSONObject2.put("mno", 2000);
        jSONArray.put(1,jSONObject2);        
        return jSONArray.toString();
    }
    public String jsonTest_2() throws JSONException{
        JSONArray jSONArray = new JSONArray();
        JSONObject jSONObject1 = new JSONObject();
        JSONObject jSONObject_1 = new JSONObject();        
        jSONObject_1.put("1", 100);        
        jSONObject_1.put("2", 200);        
        jSONObject1.put("abc", jSONObject_1);        
        
        JSONObject jSONObject2 = new JSONObject();
        JSONObject jSONObject_2 = new JSONObject();        
        jSONObject_2.put("3", 300);        
        jSONObject_2.put("4", 400);        
        jSONObject2.put("xyz", jSONObject_2);        
                
        jSONArray.put(0,jSONObject1);
        jSONArray.put(1,jSONObject2);
        
        return jSONArray.toString();
    }
}


Output :

Key_1 : abc
Value_1 : 100
Key_1 : xyz
Value_1 : 200
Key_1 : mno
Value_1 : 2000
Key_1 : pqr
Value_1 : 1000
Key_2 : abc
Value_2 : {“2″:200,”1”:100}
Key_2 : xyz
Value_2 : {“3″:300,”4”:400}

Advertisements
This entry was posted in JSON and tagged , . Bookmark the permalink.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s