当我尝试解析 JSON 时,我得到 JSONException

时间:2021-01-27 17:09:23

标签: java android json

我正在尝试解析一个本地 json 文件,我将它放在 assets 文件夹中并按照本教程进行操作:https://www.youtube.com/watch?v=h71Ia9iFWfI&t=483s&ab_channel=ProgrammingExperts

问题是我收到了 JSONException 错误..

public class MainActivity extends AppCompatActivity {
ArrayList<String> list = new ArrayList<>();
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    getJson();
}

public void getJson(){
    String json;

    try {
        InputStream is = getAssets().open("questions.json");
        int size = is.available();
        byte[] buffer = new byte[size];
        is.read();
        is.close();

        json = new String(buffer,"UTF-8");
        JSONArray jsonArray = new JSONArray(json);
        for (int i = 0; i < jsonArray.length(); i++) {
            JSONObject obj = jsonArray.getJSONObject(0);
            if(obj.getString("title") != null){
                list.add(obj.getString("title"));
                Log.d("key",obj.getString("title"));
            }
        }
        Log.d("key", list.toString());
    }catch (IOException e){e.printStackTrace(); Log.d("key","IO error");}
    catch (JSONException e){e.printStackTrace(); Log.d("key", "JSON error");}
}
}

Logcat:

2021-01-27 18:47:26.423 8201-8201/com.example.project D/key: JSON error

2021-01-27 18:47:26.437 8201-8201/com.example.project D/OpenGLRenderer: Skia GL Pipeline
2021-01-27 18:47:26.455 8201-8201/com.example.project W/example.projec: Accessing hidden method Landroid/graphics/Insets;->of(IIII)Landroid/graphics/Insets; (light greylist, linking)
2021-01-27 18:47:26.465 8201-8258/com.example.project D/HostConnection: HostConnection::get() New Host Connection established 0xdf022840, tid 8258
2021-01-27 18:47:26.467 8201-8258/com.example.project D/HostConnection: HostComposition ext ANDROID_EMU_CHECKSUM_HELPER_v1 ANDROID_EMU_native_sync_v2 ANDROID_EMU_native_sync_v3 ANDROID_EMU_native_sync_v4 ANDROID_EMU_dma_v1 ANDROID_EMU_YUV420_888_to_NV21 ANDROID_EMU_YUV_Cache ANDROID_EMU_async_unmap_buffer GL_OES_EGL_image_external_essl3 GL_OES_vertex_array_object GL_KHR_texture_compression_astc_ldr ANDROID_EMU_gles_max_version_3_0 
2021-01-27 18:47:26.490 8201-8258/com.example.project I/ConfigStore: android::hardware::configstore::V1_0::ISurfaceFlingerConfigs::hasWideColorDisplay retrieved: 0
2021-01-27 18:47:26.490 8201-8258/com.example.project I/ConfigStore: android::hardware::configstore::V1_0::ISurfaceFlingerConfigs::hasHDRDisplay retrieved: 0
2021-01-27 18:47:26.490 8201-8258/com.example.project I/OpenGLRenderer: Initialized EGL, version 1.4
2021-01-27 18:47:26.490 8201-8258/com.example.project D/OpenGLRenderer: Swap behavior 1
2021-01-27 18:47:26.490 8201-8258/com.example.project W/OpenGLRenderer: Failed to choose config with EGL_SWAP_BEHAVIOR_PRESERVED, retrying without...
2021-01-27 18:47:26.491 8201-8258/com.example.project D/OpenGLRenderer: Swap behavior 0
2021-01-27 18:47:26.514 8201-8258/com.example.project D/eglCodecCommon: setVertexArrayObject: set vao to 0 (0) 0 0
2021-01-27 18:47:26.514 8201-8258/com.example.project D/EGL_emulation: eglCreateContext: 0xe6c5c8e0: maj 3 min 0 rcv 3
2021-01-27 18:47:26.516 8201-8258/com.example.project D/EGL_emulation: eglMakeCurrent: 0xe6c5c8e0: ver 3 0 (tinfo 0xe6ce5600)
2021-01-27 18:47:26.544 8201-8258/com.example.project D/HostConnection: createUnique: call
2021-01-27 18:47:26.544 8201-8258/com.example.project D/HostConnection: HostConnection::get() New Host Connection established 0xdf0231a0, tid 8258
2021-01-27 18:47:26.546 8201-8258/com.example.project D/HostConnection: HostComposition ext ANDROID_EMU_CHECKSUM_HELPER_v1 ANDROID_EMU_native_sync_v2 ANDROID_EMU_native_sync_v3 ANDROID_EMU_native_sync_v4 ANDROID_EMU_dma_v1 ANDROID_EMU_YUV420_888_to_NV21 ANDROID_EMU_YUV_Cache ANDROID_EMU_async_unmap_buffer GL_OES_EGL_image_external_essl3 GL_OES_vertex_array_object GL_KHR_texture_compression_astc_ldr ANDROID_EMU_gles_max_version_3_0 
2021-01-27 18:47:26.546 8201-8258/com.example.project E/eglCodecCommon: GoldfishAddressSpaceHostMemoryAllocator: ioctl_ping failed for device_type=5, ret=-1
2021-01-27 18:47:26.551 8201-8258/com.example.project D/EGL_emulation: eglMakeCurrent: 0xe6c5c8e0: ver 3 0 (tinfo 0xe6ce5600)
2021-01-27 18:47:26.553 8201-8258/com.example.project D/eglCodecCommon: setVertexArrayObject: set vao to 0 (0) 3 2

1 个答案:

答案 0 :(得分:1)

InputStream.read 并没有像我认为的那样做。

        byte[] buffer = new byte[size];
        is.read();
        is.close();
        json = new String(buffer,"UTF-8");

is.read() 从输入流返回数据的第一个字节,您可以忽略它。

这里是文档: https://docs.oracle.com/javase/7/docs/api/java/io/InputStream.html#read()

您的缓冲区永远不会更新,因此始终是一个空字节数组。因此,当您从 json 创建字符串 buffer 时,您正在从一个空字节数组创建一个字符串。

这就解释了为什么您会得到 JSON error - 因为您的字符串 json 根本不是 JSON 字符串!它是空的。

看起来您真的想将 is 读入 buffer。为此,请使用 read 重载:

is.read(buffer);

这里是文档: https://docs.oracle.com/javase/7/docs/api/java/io/InputStream.html#read(byte[])

如您所料,这将用 InputStream 的内容填充 buffer

相关问题