1

My end result needs to be something like this:

    [{
      "label": "Sales",
      "color": "#9cd159",
      "data": [
        ["Jan", 27],
        ["Feb", 82],
        ["Mar", 56],
        ["Oct", 20]
      ]
    }]

my apex is as follows:

    class Report {   
        public String label;        
        public String color;
        public Data[] data;
    }
    class Data{
        public String key;
        public Integer value;
    }

my json serialize class is this:

    public String ReportOne{

            Report myReport = new Report();
            myReport.label = 'Sales';
            myReport.color = '#9cd159';

            myReport.data = new Data[]{};

            Data d1 = new Data();
            d1.key = 'Mon';
            d1.value = 13;

            myReport.data.add(d1);

            return JSON.serialize(myReport);
     }

The end result is being like this: What am I doing wrong?

{
  "color": "#9cd159",
  "data": [
    {
      "key": "Mon",
      "value": 13
    }
  ],
  "label": "Sales"
}
  • You need a List or Report if you want the JSON to be an array. – Eric Dec 18 '16 at 22:09
  • @Eric it doesnt work with List gives me something like this "data" : [ "[\"Mon\",13]", "[\"Mon\",13]", "[\"Mon\",13]" ] – raym0nd Dec 18 '16 at 22:22
3

Using JSON2Apex very useful site to bookmark you get the following output from your listed request of:

[{
      "label": "Sales",
      "color": "#9cd159",
      "data": [
        ["Jan", 27],
        ["Feb", 82],
        ["Mar", 56],
        ["Oct", 20]
      ]
    }]

Yields

// Generated by JSON2Apex http://json2apex.herokuapp.com/
//

public class JSON2Apex {

    public String label;
    public String color;
    public List<List<String>> data;


    public static JSON2Apex parse(String json) {
        return (JSON2Apex) System.JSON.deserialize(json, JSON2Apex.class);
    }
}

Depending on what you are trying to do and based on your question you may mean this:

{"reports":[{
      "label": "Sales",
      "color": "#9cd159",
      "data": [
        ["Jan", 27],
        ["Feb", 82],
        ["Mar", 56],
        ["Oct", 20]
      ]
    }]
}

Which yields this:

//
// Generated by JSON2Apex http://json2apex.herokuapp.com/
//

public class JSON2Apex {

    public List<Reports> reports;

    public class Reports {
        public String label;
        public String color;
        public List<List<String>> data;
    }


    public static JSON2Apex parse(String json) {
        return (JSON2Apex) System.JSON.deserialize(json, JSON2Apex.class);
    }
}

The problem is like I said, your question is an Unnamed array [] and to be valid JSON yet still return the [] you need to name it and then you can access the array by using xxx.reports

  • 1
    @raym0nd - Yea, you may have to do a specific parser since you are missing strings and integers and you cannot simply serialize both into a Object[] – Eric Dec 18 '16 at 23:14
1

In JSON,

[] is an array

{ } is a single object

You want an array containing a single object. But you are not serializing an array, you are serializing a single object, myReport, that has three members one of which, data, is an array. This is why you are seeing the results that look like this:

{
  color: { }
  data: [  ]
  label:  { }
}

If you want this:

[{
  color: { }
  data: [  ]
  label:  { }
}]

Then you need to serialize an array of reports that contains a single member.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.