Reputation: 141
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;
}
While retrieving json from Sample.java in Bean.java - I get the output of this format -
[{"id":"number"},{"name":"text"},{"type":"number"}]
I want to convert this to JSONObject of below format using JsonSimple library-
{"id":"number","name":"text","type":"number"} //convert to this format
I am just a beginner. Any help would be appreciated. Thank you in advance.
Upvotes: 1
Views: 524
Reputation: 221
The question is not very clear but it seems you dont want an Array . The output you are getting is an Array as you are explicitly serializing into an Array . If you dont want an Array then make a Java POJO to hold all the attributes needed and serialize that. [ ] represents an array in Json
Example what could be done
class POJO {
attribute 1;
attribute 2;
attribute 3;
}
and serialize an Object of this POJO class
Upvotes: 1
Reputation: 1888
You are instantiating with JSONArray. You need to instantiate with JSONObject
items=new JSONObject();
Also you need to use the put method instead of add method
items.put("item1", new item());
Upvotes: 1