Java 通过rest服务在JSON中动态添加property:value
我们如何通过REST服务将json属性:值作为用户的输入动态创建,而不必使用字段来保存值 基本上,目前的产出是:Java 通过rest服务在JSON中动态添加property:value,java,json,rest,properties,Java,Json,Rest,Properties,我们如何通过REST服务将json属性:值作为用户的输入动态创建,而不必使用字段来保存值 基本上,目前的产出是: { "name": "X", "price": "X", "ticker": "X", "status": "X", "supplier": "X", "attribute": [{ "key": "index", "value": "Nasdaq" },
{
"name": "X",
"price": "X",
"ticker": "X",
"status": "X",
"supplier": "X",
"attribute": [{
"key": "index",
"value": "Nasdaq"
},
{
"key": "priority",
"value": "high"
}
]
}
{
"name": "X",
"price": "X",
"ticker": "X",
"status": "X",
"supplier": "X",
"attribute": [{
"index": "Nasdaq"
},
{
"priority": "high"
}
]
}
所需输出为:
{
"name": "X",
"price": "X",
"ticker": "X",
"status": "X",
"supplier": "X",
"attribute": [{
"key": "index",
"value": "Nasdaq"
},
{
"key": "priority",
"value": "high"
}
]
}
{
"name": "X",
"price": "X",
"ticker": "X",
"status": "X",
"supplier": "X",
"attribute": [{
"index": "Nasdaq"
},
{
"priority": "high"
}
]
}
这里,index和Nasdaq是用户给定的值,这是要添加到属性列表中的键值对
使用的两个POJO:
Stock.java
@XmlRootElement
public class Stock {
public Stock() {
}
public Stock(String name, double price, String ticker, String status, String supplier, List<KV> attribute) {
super();
this.name = name;
this.price = price;
this.ticker = ticker;
this.status = status;
this.supplier = supplier;
this.attribute = attribute;
}
private String name;
private double price;
private String ticker;
private String status;
private String supplier;
private List<KV> attribute;
public List<KV> getAttribute() {
return attribute;
}
public void setAttribute(List<KV> attribute) {
this.attribute = attribute;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public double getPrice() {
return price;
}
public void setPrice(double price) {
this.price = price;
}
public String getTicker() {
return ticker;
}
public void setTicker(String ticker) {
this.ticker = ticker;
}
public String getStatus() {
return status;
}
public void setStatus(String status) {
this.status = status;
}
public String getSupplier() {
return supplier;
}
public void setSupplier(String supplier) {
this.supplier = supplier;
}
}
希望答案与rest服务中注释的使用或更改有关,而不是最后的文本处理。尝试使用java中的属性类 不需要定义对象列表,只需定义 属性属性
有关属性类如何工作的更多详细信息,请访问尝试在java中使用属性类 不需要定义对象列表,只需定义 属性属性
有关属性类如何工作的更多详细信息,请访问好的,一旦得到当前得到的响应,就可以创建Hashmap,而不是类“public class KV{}”, &Hashmap的键可以保存索引/优先级等,值可以保存Nasdaq/high 还将Hashmap变量命名为“attribute”
因此,您可以使用当前结构从API中提取数据,然后将其转换为我上面所说的数据并发送响应。好的,一旦您得到当前得到的响应,那么您可以创建一个Hashmap,而不是类“public class KV{}, &Hashmap的键可以保存索引/优先级等,值可以保存Nasdaq/high 还将Hashmap变量命名为“attribute”
因此,您可以使用当前的结构从API中提取数据,然后将其转换为我上面提到的数据并发送响应。非常感谢,通过hashmap实现了它。但是现在我面临一个问题,getAttribute返回值,但当我返回对象时返回null:attributes=[{index=Nasdaq,priority=High},{One=Two,Three=Four}]]当返回时:“attributes:[null,null]非常感谢,通过hashmap实现了它。但是现在我面临一个问题,getAttribute返回值,但当我返回对象时返回null:attributes=[{index=Nasdaq,priority=High},{One=Two,Three=Four}]]当返回时:“attributes:[null,null]你不认为像
“attribute:[{“index”:“Nasdaq”},{“priority”:“High”}这样的结构是无关的吗,如果你要重新构造它,最好有“attribute”:{“index”:“Nasdaq”,“priority”:“high”}
基本上需要它成为一个列表,每个大括号中都会有更多的数据分组在一起。你不认为像“attribute”这样的结构是不相关的吗:[{“index”:“Nasdaq”},{“priority”:“high”}
,如果你要重新构造它,最好有“attribute”:{“index”:“Nasdaq”,“priority”:“high”}
基本上需要它成为一个列表,每个大括号中都会有更多的数据分组在一起。