0

I have the model class to return a JSON output with two array values, one array containing int values and another with single quotes words.

class RGraph{
    int[] data;
    String[] labels;

    // getters and setters
}

When I return the RGraph class as JSON response from Spring controller with @ResponseBody, I get the following result as json o/p,

{
   "data":[8,46,96,1,18,65,84,13,72,60],
   "label":["Gary","Olga","Lewis","Rachel","Nathan","Matt","Kevin","Indigo","Lou","Pete"]
}

But what my expected output is,

{
   data:[12,43,64,57,49,35,75,58,94,63], 
   labels: ['Gary','Olga','Lewis','Rachel','Nathan','Matt','Kevin','Indigo','Lou','Pete']
}

In the above JSON response, I have two questions

  • How to remove the double quotes for keys? and
  • How to replace double quotes with single quotes in values?

I need this format because RGraph Chart library expects the values with single quotes for its labels and tooltips.

Lucky
  • 16,787
  • 19
  • 117
  • 151

2 Answers2

2

If you're referring to my RGraph charting library - single quotes and double quotes around strings are usually interchangeable, and keys for objects don't have to be quoted (unless they contain certain characters). Eg

...
'colors': ["red",'yellow',"#0f0",'blue'],
tooltips: ["a","b","c","d"]
...
Richard
  • 4,809
  • 3
  • 27
  • 46
  • Thanks for the reply. Yes, I was referring to RGraph library. Now it is working even with double quotes. IDK, why it din't work before. But the ajax examples provided in rgraph demos return the response with single quotes in `http://rgraph.net/getdata.html?json` so I thought the double quotes were the problem. – Lucky Jul 02 '16 at 15:01
  • I tend to use single quotes because I come from PHP - where double quotes and single quotes are slightly different. – Richard Jul 02 '16 at 15:05
0

You would have to do your own serializing/processing at the client if you want that -- the JSON format defines that keys (and string values) are quoted with double-quotes.

daf
  • 1,289
  • 11
  • 16