2011-08-30 4 views
0

나는 이미지 경로와 좌표 세트를 반환하는 간단한 JSON 피드를 가지고있다. "코즈"는 무제한의 코디네이션 세트를 가질 수 있습니다. 아래 예제에서 3 세트 만 있습니다.GSON을 사용하여 json 데이터 피드 구문 분석

{"image":"Some data", "coords": {"0":[0,0], "1":[55,22], "2":[46,65]}} 

어떻게 GSON을 사용하여 구문 분석하나요? 어떻게이 수업을 만드나요?

감사

+1

문제가 무엇입니까 ?? – Houcine

+0

문제는 없지만 몇 가지 질문 만 있습니다. – dotty

+0

편집 내용이 여전히 유효하지 않습니다 JSON. 아래 내 대답을 참조하십시오. 그리고 유효한 JSON으로 편집하면 ... 가지고있는 문제가 무엇입니까? 당신이 GSON을 어떻게 사용하는지 모른다면, 그들의 문서는 오히려 좋습니다. –

답변

2

는 그 다음 JSON은이 자습서를 참조 구문 분석 ... 즐길 수 단단한 유효한 JSON이 아니기 때문에 그 시간이 필요합니다. 이 같은

{"image":"Some data", "coords": {"0":[0,0], "1":[55,22], "2":[46,65]}}

... 유효한 JSON한다면

http://jsonlint.com/

나는 GSON이 <String, ArrayList<Integer>>mapcoords을 구문 분석 할 수 있으리라 생각하지만, 나는 확인을 시도 할 필요가 거라고 .

1

gson-1.7.1.jar 파일을 추가하고 URL에서 JSONObject 또는 JSONArray을 요구받을이 클래스를 작성합니다.

public class GetJson { 

    public JSONArray readJsonArray(String url) { 

     String read = null; 
     JSONArray mJsonArray = null; 
     try { 
      HttpClient http = new DefaultHttpClient(); 
      HttpPost post = new HttpPost(url); 
      HttpResponse response = http.execute(post); 
      HttpEntity entity = response.getEntity(); 
      InputStream is = entity.getContent(); 

      BufferedReader br = new BufferedReader(new InputStreamReader(is)); 
      StringBuilder builder = new StringBuilder(); 
      String str = null; 
      while ((str = br.readLine()) != null) { 
       builder.append(str); 
      } 
      is.close(); 
      read = builder.toString(); 
      mJsonArray = new JSONArray(read); 
     } catch (Exception e) { 
      e.printStackTrace(); 
     } 
     return mJsonArray; 
    } 

    public JSONObject readJsonObject(String url) { 
     String read = null; 
     JSONObject mJsonObject = null; 
     try { 
      HttpClient http = new DefaultHttpClient(); 
      HttpPost post = new HttpPost(url); 
      HttpResponse response = http.execute(post); 
      HttpEntity entity = response.getEntity(); 
      InputStream is = entity.getContent(); 

      BufferedReader br = new BufferedReader(new InputStreamReader(is)); 
      StringBuilder builder = new StringBuilder(); 
      String str = null; 
      while ((str = br.readLine()) != null) { 
       builder.append(str); 
      } 
      is.close(); 
      read = builder.toString(); 
      mJsonObject = new JSONObject(read); 
     } catch (Exception e) { 
      e.printStackTrace(); 
     } 
     return mJsonObject; 
    } 
} 

Tutorial 1

당신은 할 겁니다

Tutorial 2

Tutorial 3