Reputation: 35
Im pretty new with json so I dont understand what im doing wrong..
I want my data json output like this, but i
m not getting it good.
{ "data": [
["2014-01", 71173],
["2014-02", 57624],
["2014-03", 64851],
["2014-04", 60486],
["2014-05", 60500],
["2014-06", 62908],
["2014-07", 64818],
["2014-08", 59961],
["2014-09", 58542],
["2014-10", 22050]
] }
This is what I get:
{ "data": [
"hallo 0",
"hallo 10",
"hallo 20",
"hallo 30",
"hallo 40",
"hallo 50",
"hallo 60",
"hallo 70",
"hallo 80",
"hallo 90" ] }
This is the class for data with the name TestTest
public class TestTest {
@JsonProperty("data")
private List<List<Object>> data = new ArrayList<List<Object>>();
public TestTest(){
}
@JsonProperty("data")
public List<List<Object>> getData() {
return data;
}
@JsonProperty("data")
public void setData(List<List<Object>> data) {
this.data = data;
}
}
@GET
@Path("/CallsPerMinuteAsLineChart")
public Response getTest(){
TestTest test = new TestTest();
List<List<Object>> data = new ArrayList<List<Object>>();
int loop;
for(loop=0; loop < 100; loop = loop + 10){
List<Object> dataitem = new ArrayList<>();
dataitem.add("hallo");
dataitem.add(loop);
data.add(dataitem);
}
test.setData(data);
return Response.ok(test).build();
}
Upvotes: 1
Views: 974
Reputation: 2208
Sharing what I did, which I think is inline with what others have shared before me.
package general;
import java.util.ArrayList;
import java.util.List;
import org.json.JSONObject;
public class GenerateJSONout {
public static void main(String[] args) {
List<List<Object>> data = new ArrayList<List<Object>>();
int loop;
for( loop = 0; loop < 100; loop = loop + 10 ){
List<Object> dataitem = new ArrayList<>();
dataitem.add( "hallo" );
dataitem.add( loop );
data.add( dataitem );
}
JSONObject jo = new JSONObject();
jo.put("data", data);
System.out.println(jo.toString());
}
} Result:
{"data":[["hallo",0],["hallo",10],["hallo",20],["hallo",30],["hallo",40],["hallo",50],["hallo",60],["hallo",70],["hallo",80],["hallo",90]]}
Upvotes: 0
Reputation: 1053
A nice way to solve this problem is by creating serializable objects, e.g.
public class Item {
private String label;
private int value;
public Item() {
}
public Item(String label, int number) {
this.label = label;
this.number = number;
}
public void setLabel(String label) {
this.label = label;
}
public int getLabel() {
return label;
}
public void setNumber(int number) {
this.number = number;
}
public int getNumber() {
return number;
}
}
public class Data {
private List<Item> items;
public Data() {
items = new ArrayList<>();
}
public void setItems(List<Item> items) {
this.items = items;
}
public List<Item> getItems() {
return items;
}
public void addItem(Item item) {
items.add(item);
}
}
Your method:
@GET
@Path("/callsperminuteaslinechart")
public Response getTest(){
Data data = new Data();
for (int i = 10; i <= 100; i += 10) {
data.addItem(new Item("Hello", i));
}
return Response.ok(data).build();
}
Upvotes: 0
Reputation: 2924
List<Object> dataitem = new ArrayList<>();
dataitem.add( "hallo" );
dataitem.add( loop );
data.add( dataitem );
in code above you are adding a single object (dataitem) in loop and that is what you see in output. I Suggest, instead on list either use a object(Strung key, string val) type or a map ( k,v) - Possible options
1. User Map
Map<String, Object> dataitem = new HashMap<String, Object>();
Iterate the map and print object properties.
2. List of Object - where Object can have id and value as attribute that you print.
List<Object> dataitem = new ArrayList<Object>();
or Simply
3. Map<String, String> dataitem = new HashMap<String, String>();
Pring k.V for map
Upvotes: 0
Reputation: 4692
the problem is not about your json structure, although it's not a good practice to create a nested list like that, I tried your code with Gson
and Jackson
TestTest test = new TestTest();
List<List<Object>> data = new ArrayList<List<Object>>();
int loop;
for( loop = 0; loop < 100; loop = loop + 10 ){
List<Object> dataitem = new ArrayList<>();
dataitem.add( "hallo" );
dataitem.add( loop );
data.add( dataitem );
}
test.setData( data );
ObjectMapper mapper = new ObjectMapper();
String jackson = mapper.writeValueAsString( test );
System.out.println( new Gson().toJson( test ) );
System.out.println( jackson );
This code prints:
{"data":[["hallo",0],["hallo",10],["hallo",20],["hallo",30],["hallo",40],["hallo",50],["hallo",60],["hallo",70],["hallo",80],["hallo",90]]}
{"data":[["hallo",0],["hallo",10],["hallo",20],["hallo",30],["hallo",40],["hallo",50],["hallo",60],["hallo",70],["hallo",80],["hallo",90]]}
Which is exactly what you want. Since nobody explicitly solve your problem without knowing all the details I suggest you to use it like that:
@GET
@Path("/CallsPerMinuteAsLineChart")
public String getTest(){
And in this method just return the json string as I converted them.
Upvotes: 1