简体   繁体   中英

How to read from a JSON file in Java

I've been searching all night but I couldn't find anything that would work for me.

I'm trying to read and parse JSON file in Java. I tried every code I found but none worked for me. I'd greatly appreciate your help.

So here's the code:

public void parseJSONData() {

    clearData();

    try {
        FileInputStream in = openFileInput(getFilesDir()
                + "/tbl_category.json");
        InputStreamReader inputStreamReader = new InputStreamReader(in);
        BufferedReader bufferedReader = new BufferedReader(inputStreamReader);
        StringBuilder sb = new StringBuilder();
        String line;
        while ((line = bufferedReader.readLine()) != null) {
            sb.append(line);
        }

I'm using getFilesDir() + "/tbl_category.json" because the app downloads some.json file in /data/data/com.the.restaurant/files/ when it's started.

And here's the rest of the class code:

        // parse json data and store into arraylist variables
        JSONObject json = new JSONObject(line);
        JSONArray data = json.getJSONArray("data");

        for (int i = 0; i < data.length(); i++) {
            JSONObject object = data.getJSONObject(i);

            JSONObject category = object.getJSONObject("Category");

            Category_ID.add(Long.parseLong(category
                    .getString("Category_ID")));
            Category_name.add(category.getString("Category_name"));
            Category_image.add(category.getString("Category_image"));
            Log.d("Category name", Category_name.get(i));

        }

    } catch (MalformedURLException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    } catch (IOException e) {
        // TODO Auto-generated catch block
        IOConnect = 1;
        e.printStackTrace();
    } catch (JSONException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
}

I've just started learning Java and I would most greatly appreciate your help!

Instead of

 JSONObject json = new JSONObject(line);

which converts the last line read into a JSON object (and likely fails), you need

 JSONObject json = new JSONObject(sb.toString());

which would take the concatenation of lines (the contents of the StringBuilder )

In this line you are only reading in one line to create the JSON object.

 JSONObject json = new JSONObject(line);

It should use the StringBuilder which contains the entire JSON String.

 JSONObject json = new JSONObject(sb.toString());
    val inputStream: InputStream = context.assets.open(fileName)
    dataDetails = inputStream.bufferedReader().use{it.readText()}

    // now we create a json object and read the values
    val jsonObject = JSONObject(dataDetails)

    // if you know that is a number you can get it like this
    var age = jsonObj.getInt("person_age")

    // the same for boolean
    var someBooleanValue = jsonObj.getBoolean("person_married")

    // for an array
    val jsonArrayLabels = jsonObj.getJSONArray("simpleArray")
    
    //If you know what type you have to get use the function for that type, 
    //so you can save them and later do manipulations with them  

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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