How to populate data with a Json template

I have Json Format as a template (Temp.json). Below is the format of my template

{
  "products":[
    {
      "ProductTitleName": "",
      "ImageUrl":""
    }
  ]
}

Now I need to fill in the data in this format, basically the products array will have many nodes nodes. I used JsonPath expressions to retrieve the corresponding attribute value from Raw Json.My The problem is how I can use this template and populate the data in this structure.

Reason to use Template Json -

  • I used json template to avoid pojo classes
  • Although JsonPath expressions help you retrieve the necessary attributes and set them for the final attribute final Json out put result, which I build earlier, runs on the fly (Runtime).
  • , , .

, , Json , , , .

+4
1

, pojo, Map, . .

Map<String, Object> products = new HashMap<>();
List<Map<String, Object>> listProducts = new ArrayList<>();

for () { // Loop over products
    Map<String, Object> product = new HashMap<>();
    product.put("ProductTitleName", "YourTitle");
    product.put("ImageUrl", "YourImageUrl");
    listProducts.add(product);
}

products.put("products", listProducts);
// Now you can use products Map instead of an equivalent pojo class

, Velocity. , ( json ), . , , .

+4

Source: https://habr.com/ru/post/1652459/


All Articles