0

I have my class for serialize variables, but items and fields should be nodes not fields, I need to get this output:

{
  "items": [
    {
      "email": "string",
      "fields": [
        {
          "name": "string",
          "value": "string",
        }
      ]
    }
  ]
}

This is the class i have but I dont know how to implement items and fields nodes:

public class DopplerJSONSerializer {

    public String name;
    public String email;
    public String items;
    public String fields;

    public String upsertSuscribersJSON(String suscriberEmail, String suscName) {
        DopplerJSONSerializer suscWrapper = new DopplerJSONSerializer();
        suscWrapper.email = suscriberEmail;
        suscWrapper.name = suscName;
        String listJSON = JSON.serializePretty(suscWrapper, true);
        return listJSON;
    }
}

Since right now its returning:

{"name":"New Susc", "email":"[email protected]"}

1 Answer 1

1

I used JsonToApex. This is a free tool to perform such tasks.

For that structure class would be like:

public class DopplerJSONSerializer1{
    public cls_items[] items;
    class cls_items {
        public String email;    //string
        public cls_fields[] fields;
    }
    class cls_fields {
        public String name; //string
        public String value;    //string
    }
    public static DopplerJSONSerializer1 parse(String json){
        return (DopplerJSONSerializer1) System.JSON.deserialize(json, DopplerJSONSerializer1.class);
    }

    static testMethod void testParse() {
        String json=        '{'+
        '  "items": ['+
        '    {'+
        '      "email": "string",'+
        '      "fields": ['+
        '        {'+
        '          "name": "string",'+
        '          "value": "string",'+
        '        }'+
        '      ]'+
        '    }'+
        '  ]'+
        '}';
        DopplerJSONSerializer1 obj = parse(json);
        System.assert(obj != null);
    }
}

To Serialize:

DopplerJSONSerializer1 wrapper1 = new DopplerJSONSerializer1();
DopplerJSONSerializer1.cls_items items = new DopplerJSONSerializer1.cls_items();

DopplerJSONSerializer1.cls_fields fields = new DopplerJSONSerializer1.cls_fields();
fields.name = 'name';
fields.value = 'value';

items.email = '[email protected]';
items.fields = new List<DopplerJSONSerializer1.cls_fields>{ fields };

wrapper1.items = items;

JSON.serializePretty(wrapper1, true);
8
  • No, I need to move apex to json. I have those variables and I need to SERIALIZE. In my question im explaining that I need to reach that output, but I having the one that im pasting at the end. Commented Mar 17, 2017 at 18:02
  • @BoDiE2003 You can serialize just create an instance of the class generated. Assign all variables and call JSON.serializePretty(theInstance, true); Commented Mar 17, 2017 at 18:03
  • I need a code help please. I have my method there but what should I have to change to add items and fields as nodes. Commented Mar 17, 2017 at 18:05
  • @BoDiE2003 I have updated my answer, you can reference and modify the code to achieve it. Add the code in the method you have. And use passed parameters where needed. Commented Mar 17, 2017 at 18:09
  • Ohh, that makes it more clear for me, thank you very much, it guides me a lot. Commented Mar 17, 2017 at 18:10

You must log in to answer this question.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.