2011-12-05 1 views
0

가 나는 jsonencode ($ 출력)를 인쇄하는 simpe PHP 스크립트를 썼습니다. 선행 및 끝 []가있는 JSON을 출력합니다. 안드로이드 프로그램을 emulaotor를 통해 실행할 때 상태 : JSONObject 텍스트는 [.....]의 문자 1로 시작해야하며, JSON 개체는 데이터를 가져 오는 것을 알고 있으므로 문제가 있습니다. 디코딩 프로세스는 대괄호를 제거하지 않습니다., JSON 오류

public class JSONfunctions { 

public static JSONObject getJSONfromURL(String url){ 
    InputStream is = null; 
    String result = ""; 
    JSONObject jArray = null; 

    //http post 
    try{ 
      HttpClient httpclient = new DefaultHttpClient(); 
      HttpPost httppost = new HttpPost(url); 
      HttpResponse response = httpclient.execute(httppost); 
      HttpEntity entity = response.getEntity(); 
      is = entity.getContent(); 

    }catch(Exception e){ 
      Log.e("log_tag", "Error in http connection "+e.toString()); 
    } 

    //convert response to string 
    try{ 
      BufferedReader reader = new BufferedReader(new InputStreamReader(is,"iso-8859-1"),8); 
      StringBuilder sb = new StringBuilder(); 
      String line = null; 
      while ((line = reader.readLine()) != null) { 
        sb.append(line + "\n"); 
      } 
      is.close(); 
      result=sb.toString(); 
    }catch(Exception e){ 
      Log.e("log_tag", "Error converting result "+e.toString()); 
    } 

    try{ 

     jArray = new JSONObject(result);    
    }catch(JSONException e){ 
      Log.e("log_tag", "Error parsing data "+e.toString()); 
    } 

    return jArray; 
} 

}

답변

0

그것은처럼 JSON 응답 보이는 같은 소리 : [{'name':'value'},...]

귀하의 JSON 개체 오류 메시지처럼 {로 시작해야합니다 다음은 로그에 오류를 던지고 기능입니다 말한다. 그것은해야한다 :
{'my_array':[.......]}

그럼 당신은 쓸 수 : new JSONObject().getJSONArray("my_array");

+0

그러나 기본 인쇄 (jsonencode ($ 출력)); PHP에서 제목 및 대괄호없이 서식이 지정됩니다. 이것을 바꿀 방법이 없습니다. – savagenoob