【发布时间】:2017-01-12 07:10:32
【问题描述】:
Item.java
JSONObject json = new JSONObject();
private String value;
private String type;
public String getValue() {
return value;
}
public void setValue(String value) {
this.value = value;
}
public String getType() {
return type;
}
public void setType(String type) {
this.type = type;
}
public String toString() {
json.put(value, type);
return json.toString();
}
Bean.java
@PostConstruct
public void init() {
items=new JSONArray();
}
public void add() throws ParseException {
items.add(new Item());
}
public void remove(Item item) {
items.remove(item);
}
public void save() {
System.out.println("JsonSchema "+items);
}
public JSONArray getItems() {
return items;
}
从 Bean.java 中的 Sample.java 检索 json - 我得到了这种格式的输出 -
[{"id":"number"},{"name":"text"},{"type":"number"}]
我想使用 JsonSimple 库将其转换为以下格式的 JSONObject-
{"id":"number","name":"text","type":"number"} //convert to this format
我只是一个初学者。任何帮助,将不胜感激。提前谢谢你。
【问题讨论】:
-
您制作了一组对象,但您需要 1 个具有属性的对象。
-
你没有在这里展示什么是 Sample.java。如果 Sample.java 负责打印一个示例项目,您可能只需从 JSONArray 中获取第一个项目并打印它。
标签: java json json-simple