Search code examples
javaarraysjsonspringrgraph

How to get single quoted string array in JSON output?


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.


Solution

  • 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"]
    ...