Android org.json.JSONException : Value of type java. lang.String无法转换为JSONObject

标签 android json android-json

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();
      }

那时我崩溃了:

JSONObject jsonObj = new JSONObject(jsonStr);

这是我存入 SD 卡的 json 文件:

{
"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, } ] }

此 JSON 无效 "duration": 10, 末尾多了一个逗号。删除该逗号并尝试。

从每个对象中删除该逗号。修改后的 JSON 将如下所示。

{
"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 : Value of type java. lang.String无法转换为JSONObject,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21969485/

相关文章:

android - 编写自己的 Android 应用程序供个人使用?

json - Highcharts 根据值动态更改条形颜色

android - 如何在用户登录时获取 json 中的用户数据?

java - 解析 JSON 数据时出现异常

java - Android:包含用于 Retrofit2 的 SimpleXML 转换器时出现 ClassNotFoundException

android - 如何将 Twitter4j 添加到 Android 项目

java - Android JSON 转换后抛出异常(仅限设备端)

python - 如何在 Python 中检查字符串是否是有效的 JSON?

java - E/错误 JSONException : No value for categories in android

android - 使用哪个适配器?