APEX - Parsing a recursive JSON -
i programming salesforce (apex). input complex , recursive json.
{ "id":"0", "children":[ { "id":"1", "children":[...] }, { "id":"2", "children":[...] } ] }
i need way iterate complex , recursive json (in apex).
anyone can help?
thanks,
hagai
if know names of attributes in json object, can create classes represent them , deserialize. instance:
public class myobject { public string id; public list<myobject> children; }
then write method returns children
static list<myobject> getchildren(myobject parent){ list<myobject> children = new list<myobject>(); ( myobject c : parent.children ){ children.addall( getchildren(c) ); children.add(c); } return children; }
and put 2 together:
myobject parent = (myobject)json.deserialize(jsonstring,myobject.class); list<myobject> = new list<myobject>(); all.add(parent); all.addall(getchildren(parent));
Comments
Post a Comment