Android json 解析错误(Froyo 或更少)

标签 android json

Ice Cream Sandwich 、糖 bean 就可以了。 但是,它不适用于早期版本。

我不知道为什么。请给我一些建议。

jSon 数据:

{
 "ANDROID" :[
    {
        "NAME"  : "homepage",
        "URL"   : "http://www.stackoverflow.com",
        "IMAGE" : "http://www.stackoverflow/menu_01.png",
        "USE_YN"    : "Y",
        "APP_YN"    : "N"
    }
   ]        
}

错误日志:

org.json.JSONException: Value  of type java.lang.String cannot be converted to JSONObject

目标来源:

JSONObject jobject = new JSONObject(jsondata);                  

谢谢!

最佳答案

 /*"ANDROID" :[
                {
                    "NAME"  : "homepage",
                    "URL"   : "http://www.stackoverflow.com",
                    "IMAGE" : "http://www.stackoverflow/menu_01.png",
                    "USE_YN"    : "Y",
                    "APP_YN"    : "N"
                }
               ]        
            }*/

            //here is the code to parse

            try {
                String jsondata = "your server response like above statements";

                if (jsondata != null && !jsondata.equals("")
                        && jsondata.equalsIgnoreCase("null")) {
                    JSONObject jobject = new JSONObject(jsondata);

                    if (jobject != null) {
                        if (jobject.has("ANDROID")) {
                            JSONArray jsonArr = jobject.getJSONArray("ANDROID");

                            if (jsonArr != null && jsonArr.length() > 0) {
                                for (int i = 0; i < jsonArr.length(); i++) {
                                    JSONObject json = jsonArr.getJSONObject(i);

                                    if (json != null) {

                                        if (json.has("NAME")) {
                                            String name = json
                                                    .getString("NAME");
                                        }

                                        if (json.has("URL")) {
                                            String url = json.getString("URL");
                                        }

                                        if (json.has("IMAGE")) {
                                            String image = json
                                                    .getString("IMAGE");
                                        }

                                        if (json.has("USE_YN")) {
                                            String use = json
                                                    .getString("USE_YN");
                                        }

                                        if (json.has("APP_YN")) {
                                            String app = json
                                                    .getString("APP_YN");
                                        }
                                    }
                                }
                            }
                        }
                    }
                }
            } catch (Exception e) {
                e.printStackTrace();
            }

关于Android json 解析错误(Froyo 或更少),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14434876/

相关文章:

css - 选择并传递要在弹出窗口中显示的表格结果

android - 将图像与 Android 中的 JSON 数据一起上传到 Multipart 中的服务器

python - utf 8' codec can' t 在位置解码字节 0xae

javascript - 遍历 jQuery JSON 对象数组

Android - 谷歌地图路线

javascript - JSON类数组对象,添加项目

jquery - 提交包含 "???"字符串的 jQuery.ajax 数据,它将值更改为 "jQuery19107363727174233645_1373301489648?"

android - 设置 chromium depot_tools 时无法运行 fetch android

java - 如何以编程方式设计线性布局

android - 共享库依赖是如何解决的?