programming/jsp

[jsp] json-lib 를 이용한 list -> json 으로 뽑기 jdk14 [샘플]

labj 2013. 4. 3. 16:32

[jsp] json-lib 를 이용한 list -> json 으로 뽑기 jdk14 [샘플]


[애플리케이션 파일 구조]


[소스]

package sample;

import java.util.HashMap;

import java.util.List;

import java.util.Map;


import net.sf.json.JSONArray;

import net.sf.json.JSONObject; 

import net.sf.json.xml.XMLSerializer;


public class test {


/**

* @param args

* http://sheplim.tistory.com/74

*/

public static void main(String[] args) {

// TODO Auto-generated method stub

JSONArray jsonArray = JSONArray.fromObject( "['json','is','easy']" );

System.out.println("jsonArray = " + jsonArray );  

 

Map map = new HashMap();  

map.put( "name", "json" );  

map.put( "bool", Boolean.TRUE );  

map.put( "int", new Integer(1) ); 

map.put( "arr", new String[]{"a","b"} );  

JSONObject jsonObject1 = JSONObject.fromObject( map ); 

System.out.println("jsonObject1 = "+ jsonObject1 );   

 


JSONObject jsonObject = new JSONObject();

jsonObject.put("city", "Mumbai");

jsonObject.put("country1", "India");

jsonObject.put("country2", "India");

jsonObject.put("country3", "India");

jsonObject.put("country4", "India");

System.out.println("jsonObject = " + jsonObject );  

JSONObject json = new JSONObject();                 

json.accumulate("success", true);                 

json.accumulate("data", jsonObject.toString());                          

System.out.println(json.toString());                

json = json.getJSONObject("data");                             

System.out.println("json + jsonObject = "+ json.toString()); 

JSONArray jsonArray11=new JSONArray();

jsonArray11.add(0, "{'json1':'1','is':'2','easy':'3'}");

jsonArray11.add(1, "{'json2':'1','is':'2','easy':'3'}");

jsonArray11.add(2, "{'json3':'1','is':'2','easy':'3'}");

System.out.println("jsonArray11 1---> "+jsonArray11.getString(0) );  

System.out.println("jsonArray11 2---> "+jsonArray11.getString(1) );  

System.out.println("jsonArray11 3---> "+jsonArray11.getString(2) );  

System.out.println("jsonArray11 3---> "+jsonArray11.toString() );  

JSONObject json1 = new JSONObject();  

json1.accumulate("success1", true);      

json1.put("data1", jsonArray11);

System.out.println("json1 + jsonArray11 = " + json1 );  

  

}


}


[실행결과]
jsonArray = ["json","is","easy"]
jsonObject1 = {"arr":["a","b"],"int":1,"bool":true,"name":"json"}
jsonObject = {"city":"Mumbai","country1":"India","country2":"India","country3":"India","country4":"India"}
{"success":true,"data":{"city":"Mumbai","country1":"India","country2":"India","country3":"India","country4":"India"}}
json + jsonObject = {"city":"Mumbai","country1":"India","country2":"India","country3":"India","country4":"India"}
jsonArray11 1---> {"json1":"1","is":"2","easy":"3"}
jsonArray11 2---> {"json2":"1","is":"2","easy":"3"}
jsonArray11 3---> {"json3":"1","is":"2","easy":"3"}
jsonArray11 3---> [{"json1":"1","is":"2","easy":"3"},{"json2":"1","is":"2","easy":"3"},{"json3":"1","is":"2","easy":"3"}]
json1 + jsonArray11 = {"success1":true,"data1":[{"json1":"1","is":"2","easy":"3"},{"json2":"1","is":"2","easy":"3"},{"json3":"1","is":"2","easy":"3"}]}



[jsp] json-lib 를 이용한 list -> json 으로 뽑기 jdk14 [샘플]