You need to sign in to do that
Don't have an account?
Alex Makk
Json to Apex: How to process dynamic keys?
Hi everyone!
I'm having the following JSON:
I used JSON to APEX tool to get wrapper class:
The problem I'm facing is that vehicle numbers under odometers will be always different. So I can't have something like:
I'm having the following JSON:
{ "log": { "id": 776812465, "date": "2022-02-25", "vehicles": [ { "vehicle": { "id": 112334, "number": "308 R" } }, { "vehicle": { "id": 112337, "number": "#309", } } ], "odometers": { "#309": [ { "end": 83810, "start": 83810, "metric_units": false }, { "end": 83823, "start": 83810, "metric_units": false } ], "308 R": [ { "end": 194414, "start": 194310, "metric_units": false }, { "end": 194487, "start": 194414, "metric_units": false } ] } } }
I used JSON to APEX tool to get wrapper class:
public class fromJSON{ public cls_log log; class cls_log { public Integer id; //776812465 public String date; //2022-02-25 public cls_vehicles[] vehicles; public cls_odometers odometers; } class cls_vehicles { public cls_vehicle vehicle; } class cls_vehicle { public Integer id; //112334 public String number; //308 R } class cls_odometers { public cls_#309[] #309; public cls_308 R[] 308 R; } class cls_#309 { public Integer end; //83810 public Integer start; //83810 public boolean metric_units; } class cls_308 R { public Integer end; //194414 public Integer start; //194310 public boolean metric_units; } public static fromJSON parse(String json){ return (fromJSON) System.JSON.deserialize(json, fromJSON.class); } }
The problem I'm facing is that vehicle numbers under odometers will be always different. So I can't have something like:
public cls_#309[] #309; public cls_308 R[] 308 R;Please advise how I can process this dynamically via wrapper class. Thank you!
To do this, you can use a Map. Like so
Hope that helps!