【问题标题】:Get JSON value and store it获取 JSON 值并存储它
【发布时间】:2021-10-21 22:23:43
【问题描述】:

这是我的 JSON 文件,我需要从 key 中获取值:值对,然后将这些值存储到 ArrayList 评级中以对其进行排序,但是使用此代码我只是将“评论”作为键......你能帮我?我知道要获取所有 key : value 对,但我只需要一对并从中获取价值。我应该为每个使用迭代器吗?你能给我一些方向和例子吗

{
"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);
        }

    }

}

【问题讨论】:

    标签: java json arraylist


    【解决方案1】:

    主要对象只有一个键:“reviews”。它的值是一个数组。如果你想在ArrayList 中添加数组的每个元素,你可以这样做:

        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);
        }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2023-01-02
      • 2013-05-09
      • 2019-10-21
      • 2020-12-08
      • 1970-01-01
      • 1970-01-01
      • 2020-04-15
      • 1970-01-01
      相关资源
      最近更新 更多