繁体   English   中英

Android org.json.JSONException:类型java.lang.String的值无法转换为JSONObject

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

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
    }
]

}
String jsonStr = "";

代替

String jsonStr = null;

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM