testMan
testMan

Reputation: 1

Issue in converting to JSON format using Gson

I am new to JSON format.

I am trying to pass a Value for a graph in 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.

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

Upvotes: 0

Views: 72

Answers (1)

PaulProgrammer
PaulProgrammer

Reputation: 17630

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

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);

Upvotes: 1

Related Questions