2011. 4. 12. 11:09


JSONObject jObject = new JSONObject();
JSONArray jArray = new JSONArray();//배열이 필요할때

for (i=0;i>10;i++)//배열
  {
    JSONObject sObject = new JSONObject();//배열 내에 들어갈 json
    try
    {
      sObject.put("title","title");
      sObject.put("count", i);
      jArray.put(sObject);
    }
    catch (JSONException e)
    {
      log("Data json err : " + e.getMessage());
    }
}
try
{
  jObject.put("id", "newkie");
  jObject.put("datas", jArray);//배열을 넣음
}
catch (JSONException e)
{
  log("Data json err : " + e.getMessage());
}
jstring = jObject.toString();



이런식으로 하면 jstring이 {"id":"newkie","datas":[{"title":"title","count":0}.....]} 이렇게 나옴



이걸 다시 파싱



try
  {
   //받은데이터 파싱
   jObject = new JSONObject(jstring);
   
   id = jObject.getString("id");
   Log.d("id", id);
   
   JSONArray datas = jObject.getJSONArray("datas");
   int size = datas.length();
   Log.i("size", String.valueOf(size));
   
   for(int i=0; i<size; i++)
   {
    
    title = datas.getJSONObject(i).getString("title");
    Log.d("title", title);
    
    count = datas.getJSONObject(i).getString("count");
    Log.d("count", count);
    
    Title p = new Title(title,count);//지정해둔 getClass

    titlelist.add(p);리스트에 추가
       
   }
  }
  catch (Exception e)
  {
   e.getMessage()
  }


대충 이런식.

'자바' 카테고리의 다른 글

돈에 ,찍기나 전화번호에 -찍는 등의 정규식.  (0) 2011.04.21
배열정렬 Comparator  (1) 2011.03.31
간단한 쓰레드 예제  (0) 2011.03.11
암호화/복호화  (0) 2011.03.11
Posted by newkie