android - 麻烦JsonObject解析无法转换为jsonarray

标签 android json

。 我的 webservice 有一行 json 数据。所有 Json 数据都以这种格式提供,只有 URL 和链接。据我所知,它是一个 JsonObject 。 Short 说我请求,结果总是以 url 结尾。所以输出是:

{"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;
        }
         }

最佳答案

您将从响应中获取单个 Json 对象。因为 {"Url":"www.google.com"} 是一个 JSONObject。

所以这条线

JSONArray json = jParser.getJSONFromUrl(url);

应该是这样

JSONObject json = jParser.getJSONFromUrl(url);

在读取数据时,您只需要

String json_url = json.getString(TAG_Url);

而不是使用for循环。


查看更新后的类

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 了解我更新了哪些内容。

关于android - 麻烦JsonObject解析无法转换为jsonarray,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19114987/

相关文章:

Android 开发 - 使用 ArrayList<String> 与带 ArrayAdapter 的字符串数组

java - 无法解决依赖关系

java - JSONObject.append 到对象 - 结果是嵌套数组?

android - JUnit TestRule 中的 Dagger 注入(inject)

android - 重用或重新创建观察者?

android - java.lang.UnsatisfiedLinkError : dalvik. system.PathClassLoader[DexPathList.....]找不到 "libdetection_based_tracker.so"

javascript - 如何将 JSON 数据从 URL 发布到 Pubnub

php - 有没有办法通过 PHP 自动将 XML 页面转换为 JSON?

java - 如何将给定的字符串解析为有意义的 json?

javascript - 修改 PDO 返回的 JSON 对象