如何提取 JSON 数组中与特定键相关的信息?

发布于 2025-01-14 09:36:51 字数 2845 浏览 2 评论 0 原文

我有一个带有卖家信息和可用模型的移动股票 json,如下所示

{
    "title": "mobile_stock_information",
    "payload": {
        "sellers": [
            {
                "seller": "SAMSUNG",
                "phone_model": "Samsung S21"
            },
            {
                "seller": "SAMSUNG",
                "phone_model": "Samsung S21 Ultra"
            },
            {
                "seller": "SAMSUNG",
                "phone_model": "Samsung S22"
            },
            {
                "seller": "SAMSUNG",
                "phone_model": "Samsung S22 Ultra"
            },
            {
                "seller": "APPLE",
                "phone_model": "Iphone 13"
            },
            {
                "seller": "APPLE",
                "phone_model": "Iphone 13 Pro"
            },
            {
                "seller": "APPLE",
                "phone_model": "Iphone 12"
            },
            {
                "seller": "GOOGLE",
                "phone_model": "Pixel 6"
            },
            {
                "seller": "GOOGLE",
                "phone_model": "Pixel 6 XL"
            },
            {
                "seller": "GOOGLE",
                "phone_model": "Pixel 5"
            }
        ]
    }
}

我正在使用 Jayway JSON Path 从 Json 中提取所有卖家,如下所示

String sellerPath = "$.payload.sellers[*].seller";

DocumentContext jsonContext = JsonPath.parse(seller_json);

List<String> sellersList = jsonContext.read(sellerPath);

Set<String> sellerSet = new HashSet<>(sellersList);

我将列表转换为 Set 以获取唯一的卖家,之后我将迭代 JSONArray 并尝试提取移动卖家特定数据

sellersArray = sellerJSON.getJSONArray("sellers");

public static JSONObject getSellerObject(String sellerToSearch, JSONArray array) {
    for(int i = 0; i < array.length(); i++) {
        try {
            JSONObject jsonObject = array.getJSONObject(i);
            if (jsonObject.getString("seller").equals(sellerToSearch)){
                return jsonObject;
            }
        } catch (JSONException e) {
            e.printStackTrace();
        }
    }
    return null;
}

输出如下所示

{
    "title": "mobile_stock_information",
    "payload": {
        "sellers": [
            {
                "seller": "SAMSUNG",
                "phone_model": "Samsung S21"
            },
            {
                "seller": "SAMSUNG",
                "phone_model": "Samsung S21 Ultra"
            },
            {
                "seller": "SAMSUNG",
                "phone_model": "Samsung S22"
            },
            {
                "seller": "SAMSUNG",
                "phone_model": "Samsung S22 Ultra"
            }
        ]
    }
}

假设我有 5 个不同的供应商和卖家数组内的 10 个 json 对象,我必须为每个密钥迭代循环 10 次,对于 5 个密钥将是 50 次。有没有有效的方法来减少迭代次数并提取供应商特定信息。提前致谢。

I have a mobile stock json with seller information and the model available like below

{
    "title": "mobile_stock_information",
    "payload": {
        "sellers": [
            {
                "seller": "SAMSUNG",
                "phone_model": "Samsung S21"
            },
            {
                "seller": "SAMSUNG",
                "phone_model": "Samsung S21 Ultra"
            },
            {
                "seller": "SAMSUNG",
                "phone_model": "Samsung S22"
            },
            {
                "seller": "SAMSUNG",
                "phone_model": "Samsung S22 Ultra"
            },
            {
                "seller": "APPLE",
                "phone_model": "Iphone 13"
            },
            {
                "seller": "APPLE",
                "phone_model": "Iphone 13 Pro"
            },
            {
                "seller": "APPLE",
                "phone_model": "Iphone 12"
            },
            {
                "seller": "GOOGLE",
                "phone_model": "Pixel 6"
            },
            {
                "seller": "GOOGLE",
                "phone_model": "Pixel 6 XL"
            },
            {
                "seller": "GOOGLE",
                "phone_model": "Pixel 5"
            }
        ]
    }
}

I am extracting all the seller from Json using Jayway JSON Path like below

String sellerPath = "$.payload.sellers[*].seller";

DocumentContext jsonContext = JsonPath.parse(seller_json);

List<String> sellersList = jsonContext.read(sellerPath);

Set<String> sellerSet = new HashSet<>(sellersList);

I am converting the List to Set to get unique sellers and after that i am iterating through the JSONArray and trying to extract mobile seller specific data

sellersArray = sellerJSON.getJSONArray("sellers");

public static JSONObject getSellerObject(String sellerToSearch, JSONArray array) {
    for(int i = 0; i < array.length(); i++) {
        try {
            JSONObject jsonObject = array.getJSONObject(i);
            if (jsonObject.getString("seller").equals(sellerToSearch)){
                return jsonObject;
            }
        } catch (JSONException e) {
            e.printStackTrace();
        }
    }
    return null;
}

The output looks like below

{
    "title": "mobile_stock_information",
    "payload": {
        "sellers": [
            {
                "seller": "SAMSUNG",
                "phone_model": "Samsung S21"
            },
            {
                "seller": "SAMSUNG",
                "phone_model": "Samsung S21 Ultra"
            },
            {
                "seller": "SAMSUNG",
                "phone_model": "Samsung S22"
            },
            {
                "seller": "SAMSUNG",
                "phone_model": "Samsung S22 Ultra"
            }
        ]
    }
}

Let say i have 5 different vendors and 10 json object inside sellers array i have to iterate the loop for each key 10 times for 5 keys it will be 50 times. Is there any efficient way to reduce the number of iterations and extract vendor specific information. Thanks in advance.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

追风人 2025-01-21 09:36:52

您可以在 JSON 的一次传递中创建卖家/供应商及其模型的映射。您所需要做的就是编写一个 Map>,其中键是卖家,值是他们拥有的型号列表。然后,您可以检索任何特定卖家的模型列表,而无需迭代 JSON。

JSONArray sellersArray = sellerJSON.getJSONArray("sellers");
Map<String,List<String>> sellerModels = new HashMap<>();
for (int i = 0; i < sellersArray.length(); i++) {
  JSONObject modelObj = sellersArray.getJSONObject(i);
  String seller = modelObj.getString("seller");
  String model = modelObj.getString("phone_model");
  List<String> models = sellerModels.get(seller);
  if (models == null) {
    models = new ArrayList<>();
    sellerModels.put(seller, models);
  }
  models.add(model);
}

从此时起,您可以查询任何特定卖家的 sellerModels,并获取其模型列表,而无需再次迭代 JSON。

You can create in one pass of the JSON a Map of seller/vendor and their models. All you need is to do is to compose a Map<String, List<String>> where the key is the seller and the value is the list of models they have. Then you can retrieve the list of models for any specific seller without iterating over the JSON.

JSONArray sellersArray = sellerJSON.getJSONArray("sellers");
Map<String,List<String>> sellerModels = new HashMap<>();
for (int i = 0; i < sellersArray.length(); i++) {
  JSONObject modelObj = sellersArray.getJSONObject(i);
  String seller = modelObj.getString("seller");
  String model = modelObj.getString("phone_model");
  List<String> models = sellerModels.get(seller);
  if (models == null) {
    models = new ArrayList<>();
    sellerModels.put(seller, models);
  }
  models.add(model);
}

From this point you can query sellerModels for any particular seller, and get a list of its models without iterating over the JSON ever again.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文