Is it possible to serialize this JSON to Form-data in Apex?
The JSON we received:
[{"vendorcode":"4444","startDate":"2014-03-02","client":[{"name":"John"}]}]
We need to serialize it to Form-Data, and the result will be this:
data[0][vendorCode]=4444&data[0][startDate]=2014-03-02&data[0][client][0][name]=John
Right now we have this code for a simple JSON without lists inside, like client in the example above, but the problem is how to used for more levels:
List<Object> l = (List<Object>)JSON.deserializeUntyped(jsonInput);
String fields = '';
Object val;
Map<String, Object> m;
for(Integer i=0; i< l.size();i++){
m = (Map<String, Object>) l[i];
for(String key: m.keySet()){
val = m.get(key)!= null ? m.get(key):'';
fields += EncodingUtil.urlEncode('data['+String.valueOf(i)+']['+String.valueOf(key)+']', 'UTF-8');
fields +='='+ EncodingUtil.urlEncode(String.valueOf(val), 'UTF-8')+'&';
}
}
return fields;
We have this in jQuery, but i can´t find something like this in apex: