2013-10-01 139 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

相关问题