2012-03-23 220 views
0

我使用下面的Android JSON解析的代碼,但我得到的錯誤:Android的JSON解析錯誤

org.json.JSONException: End of input at character 0 of
03-23 13:54:28.905: W/System.err(1448): org.json.JSONException: End of input at character 0 of

public void fetchOriginatorName() 
    { 
     try {  
      URL url = new URL("http://financemyhome.com/webservice/bio.php"); 
      URLConnection urlconn = url.openConnection();   
      BufferedReader in = new BufferedReader(new InputStreamReader(urlconn.getInputStream())); 

      String line; 
      while ((line = in.readLine()) != null) 
      { 
       System.out.println("Line = "+line); 
       System.out.println("Length = "+line.length()); 
       JSONArray ja = new JSONArray(line); 
       for (int i = 0; i < ja.length(); i++) 
       { 
        JSONObject jo = (JSONObject) ja.get(i); 
        id.add(jo.getString("id").toString()); 
        name.add(jo.getString("first_name").toString()+jo.getString("last_name")); 
        designation.add(jo.getString("designation").toString()); 
        email.add(jo.getString("email").toString()); 
        cell.add(jo.getString("phone").toString()); 
        nmls.add(jo.getString("nmls").toString()); 
        imageurl.add(jo.getString("picture-path").toString()); 
        office.add(jo.getString("address"));   
       } 
      } 



     } catch (IOException e) { 
      // TODO Auto-generated catch block 
      e.printStackTrace(); 
     } 
    } 
+0

把你的全logcat的和你的JSON性反應的有效.... – 2012-03-23 14:03:22

回答

5

您分析您的JSON以錯誤的方式。您應該將整個json保存在字符串變量中,而不是逐行讀取,然後從JSONArray開始讀取。

例如:

JSONArray ja = new JSONArray("your_entire json_string"); 

for (int i = 0; i < ja.length(); i++) 
{ 
    JSONObject jo = (JSONObject) ja.get(i); 
    id.add(jo.getString("id").toString()); 
    name.add(jo.getString("first_name").toString()+jo.getString("last_name")); 
    designation.add(jo.getString("designation").toString()); 
    email.add(jo.getString("email").toString()); 
    cell.add(jo.getString("phone").toString()); 
    nmls.add(jo.getString("nmls").toString()); 
    imageurl.add(jo.getString("picture-path").toString()); 
    office.add(jo.getString("address"));   
} 
+0

您也可以使用*流*解析器像[傑克遜](HTTP://jackson.codehaus。組織/)。 – dmon 2012-03-23 14:09:57

+0

,或者你也可以使用像'Gson'這樣的流解析器:) – waqaslam 2012-03-23 14:10:39

+0

真正優化它,你應該在循環之前聲明jo並在每次迭代中重用它。 – 2012-03-23 14:56:05