现在的输出是:
{
"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}]
]
您能帮我构建上述结果吗?
谢谢
最佳答案
我建议您使用 JSON API 并使用它传递 DTO 对象。您需要定义一个 DTO 类,例如:
public class A {
List<ProductBrand> brands;
List<ProductType> types;
//getter and setter methods
}
然后创建该实例并设置其字段。将此类转换为 JSON 并返回它可以解决您的问题。
关于java - 按照预期从 Java Restful API 获取 JSON 格式的数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39660383/