Android的org.json.JSONException:java.lang.String类型的值不能转换为的JSONObject [英] Android org.json.JSONException: Value of type java.lang.String cannot be converted to JSONObject

查看:169
本文介绍了Android的org.json.JSONException:java.lang.String类型的值不能转换为的JSONObject的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

  {尝试
        文件yourFile =新的文件(Environment.getExternalStorageDirectory(),textarabics.txt);
        的FileInputStream流=新的FileInputStream(yourFile);
        字符串jsonStr = NULL;
        尝试{
            FileChannel FC = stream.getChannel();
            MappedByteBuffer BB = fc.map(FileChannel.MapMode.READ_ONLY,0,fc.size());            jsonStr = Charset.defaultCharset()德code(BB)的ToString()。
            Log.d(诺佳店,的jstring =+ jsonStr);
          }
          最后{
            stream.close();
          }        Log.d(诺佳店,的jstring =+ jsonStr);
             JSONObject的jsonObj =新的JSONObject(jsonStr);            //获取JSON数据节点阵列
            JSONArray数据= jsonObj.getJSONArray(数据);            //通过所有节点循环
            的for(int i = 0; I< data.length();我++){
                JSONObject的C = data.getJSONObject(I)                字符串ID = c.getString(ID);
                字符串title = c.getString(标题);
                字符串长度= c.getString(时间);                // TMP HashMap中的单个节点
                HashMap的<字符串,字符串> parsedData =新的HashMap<字符串,字符串>();                //将每个子节点的HashMap键=>值
                parsedData.put(ID,身份证);
                parsedData.put(题,题);
                parsedData.put(时间,持续时间);
                //做你想要什么你的界面上
              }
       }赶上(例外五){
       e.printStackTrace();
      }

在这一点上我得到崩溃:

 的JSONObject jsonObj =新的JSONObject(jsonStr);

这是我的JSON文件到我的SD卡:

  {
数据:
    {
        ID:1
        头衔:法尔汉·沙阿
        持续时间:10,
    },
    {
        ID:2
        头衔:诺曼·沙阿
        持续时间:10,
    },
    {
        ID:3
        头衔:艾哈迈德·沙阿
        持续时间:10,
    },
    {
        ID:4,
        头衔:穆赫辛·沙阿
        持续时间:10,
    },
    {
        ID:5,
        头衔:哈里斯沙阿
        持续时间:10,
    }
]}


解决方案

  {数据:[{ID:1,称号:法尔汉·沙阿 持续时间:10},{ID:2,称号:诺曼国王,持续时间:10},{ID:3,称号:艾哈迈德·沙阿,时间:10},{ID:4,称号:穆赫辛·沙阿,持续时间:10},{ID:5,称号:哈里斯沙,时间:10,}]}

这JSON是无效的时间:10,有在最后一个额外的逗号。删除逗号和尝试。

从每个对象删除逗号。修改后的JSON看起来是这样的。

  {
数据:
    {
        ID:1
        头衔:法尔汉·沙阿
        持续时间:10
    },
    {
        ID:2
        头衔:诺曼·沙阿
        持续时间:10
    },
    {
        ID:3
        头衔:艾哈迈德·沙阿
        持续时间:10
    },
    {
        ID:4,
        头衔:穆赫辛·沙阿
        持续时间:10
    },
    {
        ID:5,
        头衔:哈里斯沙阿
        持续时间:10
    }
]}

try {
        File yourFile = new File(Environment.getExternalStorageDirectory(), "textarabics.txt");
        FileInputStream stream = new FileInputStream(yourFile);
        String jsonStr = null;
        try {
            FileChannel fc = stream.getChannel();
            MappedByteBuffer bb = fc.map(FileChannel.MapMode.READ_ONLY, 0, fc.size());

            jsonStr = Charset.defaultCharset().decode(bb).toString();
            Log.d("Noga Store", "jString = " + jsonStr);
          }
          finally {
            stream.close();
          }

        Log.d("Noga Store", "jString = " + jsonStr);
             JSONObject jsonObj = new JSONObject(jsonStr);

            // Getting data JSON Array nodes
            JSONArray data  = jsonObj.getJSONArray("data");

            // looping through All nodes
            for (int i = 0; i < data.length(); i++) {
                JSONObject c = data.getJSONObject(i);

                String id = c.getString("id");
                String title = c.getString("title");
                String duration = c.getString("duration");

                // tmp hashmap for single node
                HashMap<String, String> parsedData = new HashMap<String, String>();

                // adding each child node to HashMap key => value
                parsedData.put("id", id);
                parsedData.put("title", title);
                parsedData.put("duration", duration);


                // do what do you want on your interface
              }


       } catch (Exception e) {
       e.printStackTrace();
      }

at that point i am getting crash:

JSONObject jsonObj = new JSONObject(jsonStr);

and this is my json file into my sd card:

{
"data": [
    {
        "id": "1",
        "title": "Farhan Shah",
        "duration": 10,
    },
    {
        "id": "2",
        "title": "Noman Shah",
        "duration": 10,
    },
    {
        "id": "3",
        "title": "Ahmad Shah",
        "duration": 10,
    },
    {
        "id": "4",
        "title": "Mohsin Shah",
        "duration": 10,
    },
    {
        "id": "5",
        "title": "Haris Shah",
        "duration": 10,
    }
]

}

解决方案

{ "data": [ { "id": "1", "title": "Farhan Shah", "duration": 10, }, { "id": "2", "title": "Noman Shah", "duration": 10, }, { "id": "3", "title": "Ahmad Shah", "duration": 10, }, { "id": "4", "title": "Mohsin Shah", "duration": 10, }, { "id": "5", "title": "Haris Shah", "duration": 10, } ] }

This JSON is invalid "duration": 10, there is an extra comma at the end. Remove that comma and try.

Remove that comma from every object. The modified JSON will look like this.

{
"data": [
    {
        "id": "1",
        "title": "Farhan Shah",
        "duration": 10
    },
    {
        "id": "2",
        "title": "Noman Shah",
        "duration": 10
    },
    {
        "id": "3",
        "title": "Ahmad Shah",
        "duration": 10
    },
    {
        "id": "4",
        "title": "Mohsin Shah",
        "duration": 10
    },
    {
        "id": "5",
        "title": "Haris Shah",
        "duration": 10
    }
]

}

这篇关于Android的org.json.JSONException:java.lang.String类型的值不能转换为的JSONObject的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆