现在的输出是:
{
"Product_Type":
"[{\"active\":true,\"description\":\"ALL IN ONES\",\"id\":2},
{\"active\":true,\"description\":\"ACCESSORIES\",\"id\":1}]",
"Product_Brand":
"[{\"active\":false,\"brand\":\"101 DALMATIANS\",\"id\":1}]"
}
预期是:
{
"Product_Type":
[{"active":true,"description":"ALL IN ONES","id":2},
{"active":true,"description":"ACCESSORIES","id":1}],
"Product_Brand":
[{"active":false,"brand":"101 DALMATIANS","id":1}]"
}
我有以下restful api代码,它返回json数据。
Code:
@GET
@Produces(MediaType.APPLICATION_JSON)
@Path("brand")
public String getProductBrand() {
return attributeMasterService.getProductBrand() +
attributeMasterService.getProductType();
}
输出:
[{"active":false,"brand":"101 DALMATIANS","id":1}]
[{"active":true,"description":"ACCESSORIES","id":1},
{"active":true,"description":"ALL IN ONES","id":2}]
我需要帮助以下面提到的格式获取数据。
基本上我需要将文档名称附加到数组中。
[
Product_Brand:
[ {"active":false,"brand":"101 DALMATIANS","id":1}],
Product_Type:
[ {"active":true,"description":"ALL IN ONES","id":2},
{"active":true,"description":"ACCESSORIES","id":1}]
]
你能否帮助我建立上述结果?
谢谢,
答案 0 :(得分:3)
我建议您使用JSON API并使用它传递DTO对象。您需要定义DTO类,例如:
public class A {
List<ProductBrand> brands;
List<ProductType> types;
//getter and setter methods
}
然后创建此实例并设置其字段。将此类转换为JSON并返回它将解决您的问题。
答案 1 :(得分:0)
使用它。
JSONObject json1 = new JSONObject();
json1.put("Product_Brand", attributeMasterService.getProductBrand());
json1.put("Product_Type", attributeMasterService.getProductType());
return json1.toString();
试试这个。
JSONObject json1 = new JSONObject();
ObjectMapper mapper = new ObjectMapper();
String jsonInString = mapper.writeValueAsString(attributeMasterService.getProductBrand());
String jsonInString1 = mapper.writeValueAsString(attributeMasterService.getProductType());
JSONParser parser = new JSONParser();
JSONArray json = (JSONArray) parser.parse(jsonInString);
JSONArray json2 = (JSONArray) parser.parse(jsonInString1);
json1.put("Product_Brand", json);
json1.put("Product_Type", json2);
return json1;