Yaniv
Yaniv

Reputation: 125

When I try to parse JSON I get JSONException

I'm trying to parse a local json file I put it in assets folder and followed this tutorial: https://www.youtube.com/watch?v=h71Ia9iFWfI&t=483s&ab_channel=ProgrammingExperts

the problem is that I get the JSONException error..

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

Upvotes: 0

Views: 37

Answers (1)

Luke Needham
Luke Needham

Reputation: 3989

InputStream.read doesn't do what I think you think it does.

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

is.read() returns the first byte of data from the inputstream, which you ignore.

Here is the documentation: https://docs.oracle.com/javase/7/docs/api/java/io/InputStream.html#read()

Your buffer is never updated, so is always an empty byte array. So when you create the string json from buffer, you're creating a string from an empty byte array.

This then explains why you get a JSON error - because your string json is not a JSON string at all! It's empty.

It looks like you really want to read is into buffer. To do that, use the read overload:

is.read(buffer);

Here is the documentation: https://docs.oracle.com/javase/7/docs/api/java/io/InputStream.html#read(byte[])

That will fill buffer with the contents of the InputStream, as you expect.

Upvotes: 1

Related Questions