简体   繁体   中英

How to extract information related a particular key inside a JSON Array?

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.

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.

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