Get JSON value and store it

Viewed 32

Here is my JSON file, I need to take values from key : value pair and then thoose values to store into ArrayList rating to sort it, but with this code I just get "reviews" as the key ... Can you help me? I know to get all key : value pairs but i need just one pair and take value from it. Should I use Iterator, for each ? Can you give me some direction and example

{
"reviews": [
  {
    "id": 2097047,
    "reviewId": "93f131be27dc1122bb7ef0048ad10e4f",
    "reviewFullText": "5 star review",
    "reviewText": "5 star review",
    "numLikes": 0,
    "numComments": 0,
    "numShares": 0,
    "rating": 5,
    "reviewCreatedOn": "2 months ago",
    "reviewCreatedOnDate": "2021-01-25T13:00:35+00:00",
    "reviewCreatedOnTime": 1611579635,
    "reviewerId": null,
    "reviewerUrl": null,
    "reviewerName": "Reviewer #20",
    "reviewerEmail": null,
    "sourceType": "custom",
    "isVerified": false,
    "source": "custom",
    "sourceName": "1-20 Reviews",
    "sourceId": "890cdd7974cdf8aabe6e9051f5a87303bdb933ae",
    "tags": [],
    "href": null,
    "logoHref": null,
    "photos": []
  },
  {
    "id": 2097046,
    "reviewId": "6e7bd4c71a56885ef583bd79186af689",
    "reviewFullText": "4 star review",
    "reviewText": "4 star review",
    "numLikes": 0,
    "numComments": 0,
    "numShares": 0,
    "rating": 4,
    "reviewCreatedOn": "2 months ago",
    "reviewCreatedOnDate": "2021-01-25T13:00:21+00:00",
    "reviewCreatedOnTime": 1611579621,
    "reviewerId": null,
    "reviewerUrl": null,
    "reviewerName": "Reviewer #19",
    "reviewerEmail": null,
    "sourceType": "custom",
    "isVerified": false,
    "source": "custom",
    "sourceName": "1-20 Reviews",
    "sourceId": "890cdd7974cdf8aabe6e9051f5a87303bdb933ae",
    "tags": [],
    "href": null,
    "logoHref": null,
    "photos": []
  },
public class GetPair {

    public static void main(String[] args) throws FileNotFoundException, IOException, ParseException {
        ArrayList<Object> rating = new ArrayList<>();
        Object obj = new JSONParser()
                .parse(new FileReader("C:\\Users\\User\\eclipse-workspace\\Task\\files\\reviews.json"));
        JSONObject jo = (JSONObject) obj;

        for (Object key : jo.keySet()) {

            String keys = (String) key;
            Object keyObj = jo.get(keys);

            if (!(keyObj instanceof JSONObject)) {

                // System.out.println(keys + " " + keyObj);
            }
            rating.add(keys);
            System.out.println(rating);
        }

    }

}
1 Answers

The main object only has one key: "reviews". Its value is an array. If you want to add each element of the array in the ArrayList, you can do something like this:

    JSONObject jo = (JSONObject) obj;
    JSONArray array = (JSONArray)jo.get("reviews");

    for (int ix = 0; ix < array.size(); ++ix) {
        JSONObject elm = (JSONObject)array.get(ix);
        rating.add(elm);
        System.out.println(rating);
    }
Related