0

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

1 Answer 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>>.

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);
Sign up to request clarification or add additional context in comments.

1 Comment

@testMan Correct, it uses the diamond operator <>. But its a simple fix to make it work on Java 6. Just specify the generic type.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.