如何在 ProdctData JSON 中填充自定义属性?
How to populate a custom attribute in ProdctData JSON?
我正在尝试根据某些请求条件获取产品数据。当我使用转换器和填充器将产品模型转换为产品数据时,我得到了所有产品数据作为响应。
我曾尝试在将 productmodel 数据转换并填充到 productdata 时设置字符串值,但没有帮助!!
{
"products": [
{
//Getting from Product Model
"name" : "ABC"
"desc" : "abcde"
"Quantity": 2"
//Not from Product Model
"matcode" : "001100"
},
]
}
是否可以在同一个响应中再设置一个字符串值(String matcode ="ABC"
)?
如果你使用的是 hql,你可以这样做:
@Entity
@Table(name = "product")
public class Product {
@Column(name = "name")
private String name;
@Column(name = "desc")
private String desc;
@Column(name = "quantity")
private Integer quantity;
@Transient
@Column(name = "quantity")
private String matcode;
public Product(String name, String desc, Integer quantity, String matcode) {
this.name = name;
this.desc = desc;
this.quantity = quantity;
this.matcode = matcode;
}
}
如果想阅读更多关于Transient
注解的内容,请关注Transient Attribute
您可以使用像 gson 这样的库。
假设您有这样的模型:
public class Products
{
private List<Product> products;
}
public class Product
{
private String name;
private String desc;
private String Quantity;
}
简单方法:
向产品模型添加另一个属性
private String matcode;
现在可以使用以下代码:
Gson gson = new Gson();
String jsonOutput = "{\"products\": [{ \"name\" : \"ABC\" ,\"desc\" : \"abcde\", \"Quantity\": \"2\"}]}";
Products products = gson.fromJson(jsonOutput, Products.class);
System.out.println(products);
for(Product p : products.getProducts()){
p.setMatcode("001100");
}
System.out.println(gson.toJson(products));
另一条更长的路径:
一个。阅读 JSON 回复
b.转换为对象(你应该已经在做)
C。使用 gson 将对象转换为 JsonElement 作为数组
d.根据需要迭代和更新 JsonObject
e.将更新后的 JsonElement 转换为 String 输出。
下面的工作代码:
Gson gson = new Gson();
String jsonOutput = "{\"products\": [{ \"name\" : \"ABC\" ,\"desc\" : \"abcde\", \"Quantity\": \"2\"}]}";
Products products = gson.fromJson(jsonOutput, Products.class);
System.out.println(products);
JsonElement jsonElement = gson.toJsonTree(products);
JsonArray jsonArray = jsonElement.getAsJsonObject().get("products").getAsJsonArray();
for (JsonElement ele : jsonArray) {
JsonObject obj = ele.getAsJsonObject();
obj.addProperty("matcode", "001100");
}
String updatedJsonOutput = gson.toJson(jsonElement);
System.out.println("Updated json Object: " + updatedJsonOutput);
理想情况下,如果您在 ProductData 中正确设置 matcode
(属性),它会反映在响应中
通过在您的 *beans.xml
中声明 ProducctData 中的 matcode 属性,例如。
<bean class="de.hybris.platform.commercefacades.product.data.ProductData">
<!-- other attributes -->
<property name="matcode" type="java.util.Set<java.lang.String>"/>
</bean>
现在在 populator 中,设置 matcode
属性值,您就完成了。调试您的控制器并查看您的自定义属性值是否存在于产品数据中。如果它在那里,那么它将正确转换为 JSON。
@Override
public void populate(final SOURCE productModel, final TARGET productData) throws ConversionException
{
//... other codes
productData.setMatcode("001100"); // add your logic to set this value
}
我正在尝试根据某些请求条件获取产品数据。当我使用转换器和填充器将产品模型转换为产品数据时,我得到了所有产品数据作为响应。
我曾尝试在将 productmodel 数据转换并填充到 productdata 时设置字符串值,但没有帮助!!
{
"products": [
{
//Getting from Product Model
"name" : "ABC"
"desc" : "abcde"
"Quantity": 2"
//Not from Product Model
"matcode" : "001100"
},
]
}
是否可以在同一个响应中再设置一个字符串值(String matcode ="ABC"
)?
如果你使用的是 hql,你可以这样做:
@Entity
@Table(name = "product")
public class Product {
@Column(name = "name")
private String name;
@Column(name = "desc")
private String desc;
@Column(name = "quantity")
private Integer quantity;
@Transient
@Column(name = "quantity")
private String matcode;
public Product(String name, String desc, Integer quantity, String matcode) {
this.name = name;
this.desc = desc;
this.quantity = quantity;
this.matcode = matcode;
}
}
如果想阅读更多关于Transient
注解的内容,请关注Transient Attribute
您可以使用像 gson 这样的库。 假设您有这样的模型:
public class Products
{
private List<Product> products;
}
public class Product
{
private String name;
private String desc;
private String Quantity;
}
简单方法:
向产品模型添加另一个属性
private String matcode;
现在可以使用以下代码:
Gson gson = new Gson();
String jsonOutput = "{\"products\": [{ \"name\" : \"ABC\" ,\"desc\" : \"abcde\", \"Quantity\": \"2\"}]}";
Products products = gson.fromJson(jsonOutput, Products.class);
System.out.println(products);
for(Product p : products.getProducts()){
p.setMatcode("001100");
}
System.out.println(gson.toJson(products));
另一条更长的路径:
一个。阅读 JSON 回复 b.转换为对象(你应该已经在做) C。使用 gson 将对象转换为 JsonElement 作为数组 d.根据需要迭代和更新 JsonObject e.将更新后的 JsonElement 转换为 String 输出。
下面的工作代码:
Gson gson = new Gson();
String jsonOutput = "{\"products\": [{ \"name\" : \"ABC\" ,\"desc\" : \"abcde\", \"Quantity\": \"2\"}]}";
Products products = gson.fromJson(jsonOutput, Products.class);
System.out.println(products);
JsonElement jsonElement = gson.toJsonTree(products);
JsonArray jsonArray = jsonElement.getAsJsonObject().get("products").getAsJsonArray();
for (JsonElement ele : jsonArray) {
JsonObject obj = ele.getAsJsonObject();
obj.addProperty("matcode", "001100");
}
String updatedJsonOutput = gson.toJson(jsonElement);
System.out.println("Updated json Object: " + updatedJsonOutput);
理想情况下,如果您在 ProductData 中正确设置 matcode
(属性),它会反映在响应中
通过在您的 *beans.xml
中声明 ProducctData 中的 matcode 属性,例如。
<bean class="de.hybris.platform.commercefacades.product.data.ProductData">
<!-- other attributes -->
<property name="matcode" type="java.util.Set<java.lang.String>"/>
</bean>
现在在 populator 中,设置 matcode
属性值,您就完成了。调试您的控制器并查看您的自定义属性值是否存在于产品数据中。如果它在那里,那么它将正确转换为 JSON。
@Override
public void populate(final SOURCE productModel, final TARGET productData) throws ConversionException
{
//... other codes
productData.setMatcode("001100"); // add your logic to set this value
}