【问题标题】:Android org.json.JSONException: Value of type java.lang.String cannot be converted to JSONObjectAndroid org.json.JSONException:java.lang.String 类型的值无法转换为 JSONObject
【发布时间】:2014-02-23 14:25:54
【问题描述】:
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,
    }
]

}

【问题讨论】:

  • 发布你的 json 字符串。
  • @RajeshCP 编辑我的回答请检查
  • Log.d("Noga Store", "jString = " + jsonStr);它在 logcat 中打印什么?
  • { "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 json android-json


【解决方案1】:
{ "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
    }
]

}

【讨论】:

  • 兄弟,我删除了每个对象中的逗号,但仍然出现崩溃 org.json.JSONException:java.lang.String 类型的值无法转换为 JSONObject
  • 现在检查日志部分 JSON 字符串是如何来的。
  • 在日志中这是现在的输出: { "data": [ { "id": "1", "title": "Farhan Shah", "duration": 10 }, { "id ": "2", "title": "Noman Shah", "duration": 3 }, { "id": "3", "title": "Ahmad Shah", "duration": 5 }, { "id ": "4", "title": "Mohsin Shah", "duration": 7 }, { "id": "5", "title": "Haris Shah", "duration": 4 } ] }跨度>
  • tutorialspoint.com/json/json_java_example.htm 关注这个关于创建 JSON 的链接。你的 JSON 仍然无效。
【解决方案2】:
String jsonStr = "";

而不是

String jsonStr = null;

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2016-08-28
    • 2012-05-09
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多