使用Gson转换为JSON格式的问题

时间:2022-09-19 18:04:17

I am new to JSON format.

我是JSON格式的新手。

I am trying to pass a Value for a graph in jQuery.

我试图在jQuery中传递一个图形的值。

The value that I have to pass is something like

我必须传递的价值是这样的

var hours = [
    ["Jan", 1],
    ["Feb", 2],
    ["Mar", 3]
];

In graph this hours is passed to data

在图表中,这个小时传递给数据

var plot_statistics = jQuery.plot($("#site_stat"), [{
    data: hours,
    label: "Hours Lost"
}]);

I tried to do this using HashMap , but i didn't got the desired output.

我尝试使用HashMap这样做,但我没有得到所需的输出。

final HashMap<String, Number> columnMap = new HashMap<String, Number>();

columnMap.put("jan", num);

Gson gson = new Gson();

gson.toJson(columnMap);

Please Help me to resolve this

请帮我解决这个问题

1 个解决方案

#1


1  

[] is a list in JSON, so your prototype has a list of lists. From that description, what you want is List<List<Object>>.

[]是JSON中的列表,因此您的原型具有列表列表。从那个描述中,你想要的是List >。

List<List<Object>> outer = new ArrayList<>();
List<Object> inner = new ArrayList<>();

inner.add("Jan");
inner.add(1);
outer.add(inner);

inner = new ArrayList<>();
inner.add("Feb");
inner.add(2);
outer.add(inner);

inner = new ArrayList<>();
inner.add("Mar");
inner.add(3);
outer.add(inner);

Gson gson = new Gson();
gson.toJson(outer);

#1


1  

[] is a list in JSON, so your prototype has a list of lists. From that description, what you want is List<List<Object>>.

[]是JSON中的列表,因此您的原型具有列表列表。从那个描述中,你想要的是List >。

List<List<Object>> outer = new ArrayList<>();
List<Object> inner = new ArrayList<>();

inner.add("Jan");
inner.add(1);
outer.add(inner);

inner = new ArrayList<>();
inner.add("Feb");
inner.add(2);
outer.add(inner);

inner = new ArrayList<>();
inner.add("Mar");
inner.add(3);
outer.add(inner);

Gson gson = new Gson();
gson.toJson(outer);