JsonArray 输出不起作用
JsonArray output is not working
我是 json 的新手,所以我不明白我m doing wrong..
I want my data json output like this, but i
我没搞清楚什么。
{ "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]
] }
这是我得到的:
{ "data": [
"hallo 0",
"hallo 10",
"hallo 20",
"hallo 30",
"hallo 40",
"hallo 50",
"hallo 60",
"hallo 70",
"hallo 80",
"hallo 90" ] }
这是名为 TestTest
的数据的 class
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();
}
问题不在于您的 json 结构,虽然创建这样的嵌套列表不是一个好的做法,但我尝试了您的代码 Gson
和 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 );
此代码打印:
{"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]]}
这正是您想要的。由于没有人在不知道所有细节的情况下明确解决您的问题,因此我建议您这样使用它:
@GET
@Path("/CallsPerMinuteAsLineChart")
public String getTest(){
并且在这个方法中只是 return 我转换的 json 字符串。
List<Object> dataitem = new ArrayList<>();
dataitem.add( "hallo" );
dataitem.add( loop );
data.add( dataitem );
在上面的代码中,您在循环中添加了一个对象(数据项),这就是您在输出中看到的内容。我建议,而不是在列表上使用对象(字符串键,字符串 val)类型或映射(k,v) - 可能的选项
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
解决这个问题的一个好方法是创建可序列化的对象,例如
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);
}
}
你的方法:
@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();
}
分享我所做的,我认为这与我之前其他人分享的内容一致。
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());
}
}
结果:
{"data":[["hallo",0],["hallo",10],["hallo",20],["hallo",30],["hallo",40],["hallo",50],["hallo",60],["hallo",70],["hallo",80],["hallo",90]]}
我是 json 的新手,所以我不明白我m doing wrong..
I want my data json output like this, but i
我没搞清楚什么。
{ "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]
] }
这是我得到的:
{ "data": [
"hallo 0",
"hallo 10",
"hallo 20",
"hallo 30",
"hallo 40",
"hallo 50",
"hallo 60",
"hallo 70",
"hallo 80",
"hallo 90" ] }
这是名为 TestTest
的数据的 classpublic 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();
}
问题不在于您的 json 结构,虽然创建这样的嵌套列表不是一个好的做法,但我尝试了您的代码 Gson
和 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 );
此代码打印:
{"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]]}
这正是您想要的。由于没有人在不知道所有细节的情况下明确解决您的问题,因此我建议您这样使用它:
@GET
@Path("/CallsPerMinuteAsLineChart")
public String getTest(){
并且在这个方法中只是 return 我转换的 json 字符串。
List<Object> dataitem = new ArrayList<>();
dataitem.add( "hallo" );
dataitem.add( loop );
data.add( dataitem );
在上面的代码中,您在循环中添加了一个对象(数据项),这就是您在输出中看到的内容。我建议,而不是在列表上使用对象(字符串键,字符串 val)类型或映射(k,v) - 可能的选项
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
解决这个问题的一个好方法是创建可序列化的对象,例如
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);
}
}
你的方法:
@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();
}
分享我所做的,我认为这与我之前其他人分享的内容一致。
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());
}
} 结果:
{"data":[["hallo",0],["hallo",10],["hallo",20],["hallo",30],["hallo",40],["hallo",50],["hallo",60],["hallo",70],["hallo",80],["hallo",90]]}