2013-10-01 72 views
2

。 我有一行json數據@我的web服務。所有Json數據均以此格式提供,僅限URL和鏈接。所以它是我知道的JsonObject。 Short說我要求,結果總是以url結尾。所以輸出是:問題JsonObject解析無法轉換爲jsonarray

{"Url":"www.google.com"} 

這是我做了

  JSONArray json = jParser.getJSONFromUrl(url); 

      try { 
       ListBasedList.clear(); 
       //for each loop til JSON data 
        for(int i = 0; i < json.length(); i++){ 
         JSONObject c = json.getJSONObject(i); 

        String json_url = c.getString(TAG_Url); 

        if(json_url.equals(0) && json_url.equals("")) 
        { 
         LinearLayout lin_footer = (LinearLayout) findViewById(R.id.footer_layoutMain); 
         lin_footer.setVisibility(View.GONE); 
        } 

        HashMap<String, String> map = new HashMap<String, String>(); 
        map.put(TAG_Url, json_url); 

        ListBasedList.add(map); 
        } 
      } catch (JSONException e) { 
       e.printStackTrace(); 
       Log.e("JSON Parser fejl", "fejl da man prøve og hente data fra server " + e.toString()); 
      } 
      return null; 
     } 

錯誤發生在這裏

logcat的告訴我:

的JSONObject不能轉換到jsonarray

那麼我怎麼能有鏈接而不是錯誤?

更新#1 - > logcat的完整的錯誤

10-01 13:34:45.685: E/JSON Parser(24256): Error parsing data org.json.JSONException: Value {"url":"www.google.com"} of type org.json.JSONObject cannot be converted to JSONArray 

更新#2 - > JsonParser類

public class JSONParser { 
    static InputStream is = null; 
    static String json = ""; 
    JSONArray jsonarr=null; 
    // konstruktor 
    public JSONParser() { 
    } 

    public JSONArray getJSONFromUrl(String url) { 
     JSONArray jsonarr=null; 

     // HTTP request 
     try { 
      // defaultHttpClient 
      DefaultHttpClient httpClient = new DefaultHttpClient(); 
      HttpPost httpPost = new HttpPost(url); 

      HttpResponse httpResponse = httpClient.execute(httpPost); 
      HttpEntity httpEntity = httpResponse.getEntity(); 
      is = httpEntity.getContent();   

     } catch (UnsupportedEncodingException e) { 
      e.printStackTrace(); 
     } catch (ClientProtocolException e) { 
      e.printStackTrace(); 
     } catch (IOException e) { 
      e.printStackTrace(); 
     } 
     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(); 
      json = sb.toString(); 
     } catch (Exception e) { 
      Log.e("Buffer Error", "Error converting result " + e.toString()); 
     } 

     // json array paser til string 
      try { 
       jsonarr = new JSONArray(json); 
       } catch (JSONException e) { 
      Log.e("JSON Parser", "Error parsing data " + e.toString()); 
     } 

     // retunerer json object tilbage 
     return jsonarr; 
     } 
     } 
+0

安置自己的JSON響應。 –

+0

向我們展示您的json – Raghunandan

+0

{「Url」:「www.google.com」} – Tirolel

回答

1

您是從響應得到一個JSON對象。由於{"Url":"www.google.com"}是一個JSONObject。

所以行

JSONArray json = jParser.getJSONFromUrl(url); 

應儘可能

JSONObject json = jParser.getJSONFromUrl(url); 

雖然讀取數據,你只需要

String json_url = json.getString(TAG_Url); 

,而不是使用循環。


看到更新後的類

public class JSONParser { 
    static InputStream is = null; 
    static String json = ""; 
    JSONObject jsonObject = null; // Updated here 

    // konstruktor 
    public JSONParser() { 
    } 

    public JSONObject getJSONFromUrl(String url) { 
     jsonObject = null; // Updated here 

     // HTTP request 
     try { 
      // defaultHttpClient 
      DefaultHttpClient httpClient = new DefaultHttpClient(); 
      HttpPost httpPost = new HttpPost(url); 

      HttpResponse httpResponse = httpClient.execute(httpPost); 
      HttpEntity httpEntity = httpResponse.getEntity(); 
      is = httpEntity.getContent(); 

     } catch (UnsupportedEncodingException e) { 
      e.printStackTrace(); 
     } catch (ClientProtocolException e) { 
      e.printStackTrace(); 
     } catch (IOException e) { 
      e.printStackTrace(); 
     } 
     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(); 
      json = sb.toString(); 
     } catch (Exception e) { 
      Log.e("Buffer Error", "Error converting result " + e.toString()); 
     } 

     // json array paser til string 
     try { 
      jsonObject = new JSONObject(json); // Updated here 
     } catch (JSONException e) { 
      Log.e("JSON Parser", "Error parsing data " + e.toString()); 
     } 

     // retunerer json object tilbage 
     return jsonObject; // Updated here 
    } 
} 

這將爲當前的JSON工作。請參閱// Updated here以瞭解我已更新的內容。

+0

我收到此錯誤:類型不匹配:無法從JSONArray轉換爲JSONObject。 – Tirolel

+0

@Tirolel是的,你會得到。看到我更新的答案。您還需要更改循環邏輯。 –

+0

謝謝。我剛剛更新了這個問題。 – Tirolel

1

你得到的是一個JSONObject不是一個JSONArray。

{ // represetns json object node 
"Url":"www.google.com" 
} 

SO改變

JSONObject jsonobject= jParser.getJSONFromUrl(url); 

爲了解析

String url = jsonobject.getString("Url"); 

編輯:

您需要更改這個

jsonarr = new JSONArray(json); 

JSONObject job = new JSONObject(json); // considering you get the above json 

,並返回

return job; 
+0

我收到此錯誤:類型不匹配:無法從JSONArray轉換爲JSONObject – Tirolel

+0

發佈完整的json併發布堆棧跟蹤 – Raghunandan

+1

@Tirolel他的回答是正如你想解析的JSON一樣。如果你還沒有發佈完整的JSON,那麼沒有人可以幫助你,你應該嘗試瞭解別人的嘗試和時間。 –

1

您在JSONParser類中的錯誤,您正在返回JSONArray而您試圖獲得JSONObeject。請參閱以下代碼。

public class JSONParser { 
    static InputStream is = null; 
    static String json = ""; 
    JSONObject jsonarr=null; 
    // konstruktor 
    public JSONParser() { 
    } 

    public JSONObject getJSONFromUrl(String url) {// Change return type from `JSONArray` to `JSONObject` 
     JSONObject jsonarr=null; 

     // HTTP request 
     try { 
      // defaultHttpClient 
      DefaultHttpClient httpClient = new DefaultHttpClient(); 
      HttpPost httpPost = new HttpPost(url); 

      HttpResponse httpResponse = httpClient.execute(httpPost); 
      HttpEntity httpEntity = httpResponse.getEntity(); 
      is = httpEntity.getContent();   

     } catch (UnsupportedEncodingException e) { 
      e.printStackTrace(); 
     } catch (ClientProtocolException e) { 
      e.printStackTrace(); 
     } catch (IOException e) { 
      e.printStackTrace(); 
     } 
     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(); 
      json = sb.toString(); 
     } catch (Exception e) { 
      Log.e("Buffer Error", "Error converting result " + e.toString()); 
     } 

     // json array paser til string 
      try { 
       jsonarr = new JSONObject(json); 
       } catch (JSONException e) { 
      Log.e("JSON Parser", "Error parsing data " + e.toString()); 
     } 

     // retunerer json object tilbage 
     return jsonarr; 
     } 
     } 

然後使用下面的代碼。

JSONObject json = jParser.getJSONFromUrl(url); 

String url = json.getString("url"); 
+0

非常感謝! – Tirolel

相關問題