0

Can anyone help me understand on how to parse the response in to a JSON object from string.

String s = [{"id":"12345","Account":"31338","name":"Accounttest","subaccounts":[{"id":"39658"}]}]

I'm generating the above string Using JSON.createGenerator.

0

You say you want an Object so it is pretty easy:

var result = JSON.parse(s);

Or do you mean you want it to be parsed into a class or object in apex:

Map<String,Object> result = JSON.deserializeUntyped(s);

Into a class You can use JSON2Apex to generate the class:

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

public class JSON2Apex {

    public class Subaccounts {
        public String id;
    }

    public String id;
    public String Account;
    public String name;
    public List<Subaccounts> subaccounts;


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

If none if this is what you are looking for, could could you clarify your question

  • Do we have JSON.Parse method in APEX? – cartman Dec 9 '15 at 6:42
  • @cartman - No, only serialize and deserialize, thats why I put the latter part of my answer for, in case you wanted to do it in apex. You could also use the JSONParser methods but it is overkill for what you are doing here. – Eric Dec 9 '15 at 6:44

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