Errors building JSON string with Java for Google Visualization API

117 views
Skip to first unread message

ah89

unread,
Dec 20, 2012, 5:34:10 AM12/20/12
to google-visua...@googlegroups.com
Hey,

I am trying to convert an array to a JSON string, which can be assigned to a Google Visualization datatable.
However, whatever I try, I keep getting errors. Even with the example of Google Visualization API!

This is what I have:

String haha =  "{" +
                    "       cols: [{id: 'task',  label: 'Task',          type: 'string'}," +
                    "              {id: 'hours', label: 'Hours per Day', type: 'number'}]," +
                    "       rows: [{c:[{v: 'Work'},     {v: 11}]}," +
                    "              {c:[{v: 'Eat'},      {v: 2}]}," +
                    "              {c:[{v: 'Commute'},  {v: 2}]}," +
                    "              {c:[{v: 'Watch TV'}, {v:2}]}," +
                    "              {c:[{v: 'Sleep'},    {v:7, f:'7.000'}]}" +
                    "             ]" +
                    "     }";


JSP:  var view = new google.visualization.DataTable(${chartData});

What I get is an error, telling me that the JSON format is invalid.

The goal is to save a hashmap to a datatable with JSON (hashmap contains a category and a value). The above example was used as I cannot seem to build a proper JSON format myself. But this doesn't work as well.

Hopefully someone can help me. Thanks in advance

Daniel LaLiberte

unread,
Dec 20, 2012, 8:44:16 AM12/20/12
to google-visua...@googlegroups.com
Howdy,

The requirements for proper JSON syntax are strict, and not the same as JavaScript Object literals.  The names of properties must be quoted with double quotes, so you might prefer to use single quotes for the outermost string quoting and double quotes inside.  E.g.
var haha = 
  '{' +
  '  "cols":[{"id": "task", ...}]' +
  '}';

dan

--
You received this message because you are subscribed to the Google Groups "Google Visualization API" group.
To view this discussion on the web visit https://groups.google.com/d/msg/google-visualization-api/-/c4MMELWfEekJ.
To post to this group, send email to google-visua...@googlegroups.com.
To unsubscribe from this group, send email to google-visualizati...@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/google-visualization-api?hl=en.



--
dlaliberte@Google.com   562D 5CC, Cambridge MA
daniel.laliberte@GMail.com 9 Juniper Ridge Road, Acton MA

asgallant

unread,
Dec 20, 2012, 1:06:44 PM12/20/12
to google-visua...@googlegroups.com
JSON specifies that all strings are quoted with double-quotes, so you need to do that with the internal strings as well as the properties.
dan

To unsubscribe from this group, send email to google-visualization-api+unsub...@googlegroups.com.

For more options, visit this group at http://groups.google.com/group/google-visualization-api?hl=en.
dlali...@Google.com   562D 5CC, Cambridge MA
daniel.l...@GMail.com 9 Juniper Ridge Road, Acton MA
Reply all
Reply to author
Forward
0 new messages