Force.com에서 JSON 처리

apex-library의 JSONObject를 사용하면 Apex에서 JSON 데이터를 처리할 수 있습니다. 가장 중요한 Class는 JSONObject Class와 JSONObject.value Class 입니다.


JSONObject Class

* 생성자
   JSONObject(String source)
   사용 예)
       JSONObject  json  =  new JSONObject('여기에 json 문자열');

* Key 존재 여부 확인
   Boolean  has(String key)

* Key로 데이터 읽기
   String   getString(String key)
   사용 예)
       String  value  =  json.getString('name');
       Integer  intValue  =  Integer.valueOf(json.getString('nameInt'));
       Date  dateValue  =  Date.valueOf(json.getString('nameDate'));
       Boolean  boleanValue = Boolean.valueOf(json.getString('nameBoolean'));

* Key로 배열 데이터 읽기
   List<JSONObject.value>  data  =  json.getValue('data').values;
   사용 예)
       for (JSONObject.value  val  :  json.getValue('data').values) {
           JSONObject item = null;
           item = new JSONObject(jsonItem.valueToString());
           //--- TODO: 여기에 item을 사용하는 프로그램을 작성 하세요.
       }

* JSON 문자열 생성 사례
   JSONObject  json  =  new JSONObject('{}');
   json.putOpt('nameString',  new  JSONObject.value('문자열'));
   json.putOpt('nameInteger',  new  JSONObject.value(777));
   String strJson  =  json.valueToString();
 

오픈소스 비즈니스 컨설팅


Posted by 산사랑

2011/06/10 13:03 2011/06/10 13:03
, , ,
Response
No Trackback , No Comment
RSS :
http://www.jopenbusiness.com/tc/oss/rss/response/338

Trackback URL : http://www.jopenbusiness.com/tc/oss/trackback/338

Leave a comment
[로그인][오픈아이디란?]
Android에서 JSON 데이터를 송수신하기 위해서 HttpURLConnection을 사용하여 만든 함수 입니다.

  • serverURL : JSON 요청을 받는 서버의 URL
  • postPara : POST 방식으로 전달될 입력 데이터
  • flagEncoding : postPara 데이터의 URLEncoding 적용 여부
  • 반환 데이터 : 서버에서 전달된 JSON 데이터

    public static String getJson(String serverUrl, String postPara, boolean flagEncoding) throws Exception {
        URL url = null;
        HttpURLConnection conn = null;
        PrintWriter postReq = null;
        BufferedReader postRes = null;
        StringBuilder json = null;
        String line = null;
        
        json = new StringBuilder();
        try {
            if (flagEncoding) {
                postPara = URLEncoder.encode(postPara);
            }
            
            url = new URL(serverUrl);
            conn = (HttpURLConnection) url.openConnection();
            conn.setDoOutput(true);
            conn.setUseCaches(false);
            conn.setRequestMethod("POST");
            conn.setRequestProperty("Content-Type", "text/plain");
            conn.setRequestProperty("Content-Length",
                                                       Integer.toString(postPara.length()));
            conn.setDoInput(true);
            
            postReq = new PrintWriter(
                              new OutputStreamWriter(conn.getOutputStream(), "UTF-8"));
            postReq.write(postPara);
            postReq.flush();
            
            postRes = new BufferedReader(
                             new InputStreamReader(conn.getInputStream(), "UTF-8"));
            while ((line = postRes.readLine()) != null){
                json.append(line);
            }
            conn.disconnect();
        } catch (MalformedURLException ex) {
            throw new Exception(ex.getMessage());
        } catch (IOException ex) {
            throw new Exception(ex.getMessage());
       } catch (Exception ex) {
            throw new Exception(ex.getMessage());
       }
        return json.toString();    
    }


Posted by 산사랑

2011/03/08 00:40 2011/03/08 00:40
, , , , ,
Response
No Trackback , No Comment
RSS :
http://www.jopenbusiness.com/tc/oss/rss/response/304

Trackback URL : http://www.jopenbusiness.com/tc/oss/trackback/304

Leave a comment
[로그인][오픈아이디란?]

블로그 이미지

개인적인 글쓰기와 오픈소스 비즈니스 컨설팅 관련 글을 정리합니다. consult (골뱅이) jopenbusiness.com

- 산사랑

Archives

12명이 RSS를 구독하고 있습니다.

Site Stats

Total hits:
533130
Today:
687
Yesterday:
862

*** 방문자 통계 ***
0612 : (572)
0613 : (620)
0614 : (569)
0615 : (518)
0616 : (712)
0617 : (736)
0618 : (862)
0619 : (687)
7일간 총 방문자수 : 4589