为什么我看不懂json

时间:2019-08-27 10:18:33

标签: android json

我正在尝试读取JSON,但出现以下错误。

2019-08-27 13:12:43.352 27379-27379/com.test W/System.err: org.json.JSONException: Value transcript of type java.lang.String cannot be converted to JSONObject

JSON

{"transcript":{"text":[{"content":"Flood!","dur":"1.76","start":"2.28"},{"content":"Listen up, there’s a flood coming!","dur":"2.24","start":"4.52"},{"content":"Oy lads, flood!","dur":"1.92","start":"7.88"},{"content":"There’s a flood –","dur":"1.24","start":"10.72"},{"content":"Maurice, they’re not listening.","dur":"1.84","start":"13.96"},{"content":"We’ve got to give them\na good old-fashioned fish slappin’.","dur":"2.8","start":"15.96"},{"content":"Fish Slap!","dur":"1.04","start":"20.52"},{"content":"Fish Slap!","dur":"1","start":"22.32"},{"content":"Fish Slap!","dur":"1.2","start":"24.12"},{"content":"Double Fish Slap!","dur":"1.24","start":"25.84"},{"content":"See, guys? Flood’s here.","dur":"1.56","start":"27.84"},{"content":"Tub Rider!","dur":"2.48","start":"31.56"},{"content":"SEASON 1\nTHE FLOOD","dur":"1","start":"35.32"},{"content":"Because I’m in a tub!","dur":"1.04","start":"36.48"},{"content":"PREPARE TO BATTLE ON A BOAT","dur":"1.48","start":"37.68"}]}}

代码

    try {
            JSONObject jsonObject = new JSONObject("transcript");
            JSONArray jsonArray = new JSONArray("text");
            Log.w("aaa",String.valueOf(jsonArray.getJSONObject(0).get("content")));
        } catch (JSONException e) {
            e.printStackTrace();
        }

5 个答案:

答案 0 :(得分:2)

将您的json放入字符串,然后尝试解析它:

String json = "{\"transcript\":{\"text\":[{\"con ...}";
try {
    JSONObject jsonObject = new JSONObject(json);
    JSONObject jsonObject = new JSONObject("transcript");
    JSONArray jsonArray = new JSONArray("text"); 
    Log.w("aaa",String.valueOf(jsonArray.getJSONObject(0).get("content")));
} catch (JSONException e) {
    e.printStackTrace();
}

记住要用反斜杠转义双引号

答案 1 :(得分:0)

String response = "{"transcript":{"text":[{"content":"Flood!","dur":"1.76","start":"2.28"},{"content":"Listen up, there’s a flood coming!","dur":"2.24","start":"4.52"},{"content":"Oy lads, flood!","dur":"1.92","start":"7.88"},{"content":"There’s a flood –","dur":"1.24","start":"10.72"},{"content":"Maurice, they’re not listening.","dur":"1.84","start":"13.96"},{"content":"We’ve got to give them\na good old-fashioned fish slappin’.","dur":"2.8","start":"15.96"},{"content":"Fish Slap!","dur":"1.04","start":"20.52"},{"content":"Fish Slap!","dur":"1","start":"22.32"},{"content":"Fish Slap!","dur":"1.2","start":"24.12"},{"content":"Double Fish Slap!","dur":"1.24","start":"25.84"},{"content":"See, guys? Flood’s here.","dur":"1.56","start":"27.84"},{"content":"Tub Rider!","dur":"2.48","start":"31.56"},{"content":"SEASON 1\nTHE FLOOD","dur":"1","start":"35.32"},{"content":"Because I’m in a tub!","dur":"1.04","start":"36.48"},{"content":"PREPARE TO BATTLE ON A BOAT","dur":"1.48","start":"37.68"}]}}"

       try {
            JSONObject jsonObject = new JSONObject(response);
            JSONObject transcript = jsonObject.getJSONObject("transcript")
           JSONArray jsonArray = transcript.getJSONArray("text");
            Log.w("aaa",String.valueOf(jsonArray.getJSONObject(0).get("content")));
        } catch (JSONException e) {
            e.printStackTrace();
        }

答案 2 :(得分:0)

从您提供的问题和代码中,我了解到您是新手,不熟悉Android中的JSON API。为此,我建议您阅读官方文档并熟悉 org.json API(JSONObjectJSONArray):

https://developer.android.com/reference/org/json/JSONObject https://developer.android.com/reference/org/json/JSONArray

之后,您可能想使用 org.json API在Android中练习并遵循一些有关JSON序列化/反序列化的教程。这是我在网上为您找到的相当完整的教程(仅作为示例,实际上有数百个):

https://www.javacodegeeks.com/2013/10/android-json-tutorial-create-and-parse-json-data.html

答案 3 :(得分:0)

代替String.valueOf(jsonArray.getJSONObject(0).get("content"))

您可以使用: jsonArray.getJSONObject(0).getString("content")

答案 4 :(得分:0)

您需要先将json字符串传递给Json对象。

请参考以下代码,它可能对您有用。

 try {
        JSONObject obj = new JSONObject("{\"transcript\":{\"text\":[{\"content\":\"Flood!\",\"dur\":\"1.76\",\"start\":\"2.28\"},{\"content\":\"Listen up, there�s a flood coming!\",\"dur\":\"2.24\",\"start\":\"4.52\"},{\"content\":\"Oy lads, flood!\",\"dur\":\"1.92\",\"start\":\"7.88\"},{\"content\":\"There�s a flood �\",\"dur\":\"1.24\",\"start\":\"10.72\"},{\"content\":\"Maurice, they�re not listening.\",\"dur\":\"1.84\",\"start\":\"13.96\"},{\"content\":\"We�ve got to give them\\na good old-fashioned fish slappin�.\",\"dur\":\"2.8\",\"start\":\"15.96\"},{\"content\":\"Fish Slap!\",\"dur\":\"1.04\",\"start\":\"20.52\"},{\"content\":\"Fish Slap!\",\"dur\":\"1\",\"start\":\"22.32\"},{\"content\":\"Fish Slap!\",\"dur\":\"1.2\",\"start\":\"24.12\"},{\"content\":\"Double Fish Slap!\",\"dur\":\"1.24\",\"start\":\"25.84\"},{\"content\":\"See, guys? Flood�s here.\",\"dur\":\"1.56\",\"start\":\"27.84\"},{\"content\":\"Tub Rider!\",\"dur\":\"2.48\",\"start\":\"31.56\"},{\"content\":\"SEASON 1\\nTHE FLOOD\",\"dur\":\"1\",\"start\":\"35.32\"},{\"content\":\"Because I�m in a tub!\",\"dur\":\"1.04\",\"start\":\"36.48\"},{\"content\":\"PREPARE TO BATTLE ON A BOAT\",\"dur\":\"1.48\",\"start\":\"37.68\"}]}}");
        JSONObject transcriptObject = obj.getJSONObject("transcript");
        Log.d("transcript object",transcriptObject.toString());
        JSONArray textArray = transcriptObject.getJSONArray("text");
        Log.d("text array",textArray.toString());
    } catch (Exception e) {
        e.printStackTrace();
    }

您可以使用GSON库进行json解析。