I am new in using JSONParser class, I am struggling a bit to understand how to use it.
The string that I want to use this, is a reponse from an API call, it is a really long string (about 5.4 million characters), so i guess this is the best way to process this response.
The response looks like the json below, where I am not interested in VALUE2 and VALUE4. I would like to process VALUE1 and VALUE3. I have created an object called Distribution__c, with the fields: PKT, CODE, COLOR, STATUS, WEIGHT, YEARS, LOC and SHOP, FABRIC, CITY, PLOT
and the purpose is to each time the api is call it inserts this values into distribution.
In the below example VALUE1, has 2 arrays in it, but this value always changes
{
"VALUE1": [
{
"PKT": "0.00",
"CODE": "1299600",
"COLOR": "GREEN",
"STATUS": "GOOD",
"WEIGHT": "6211",
"YEARS": "5",
"LOC": "SYD"
}
{
"PKT": "0.01",
"CODE": "129400",
"COLOR": "RED",
"STATUS": "POOR",
"WEIGHT": "621",
"YEARS": "1",
"LOC": "SYD"
}
],
"VALUE2": [
"PKT",
"CODE",
"COLOR",
"STATUS",
"WEIGHT",
"YEARS",
"LOC"
],
"VALUE3": {
"SHOP": "https://s3.amazonaws.com/SHOP/",
"FABRIC": "https://s3.amazonaws.com/FABRIC/",
"CITY": "http://s3.amazonaws.com/CITY/",
"PLOT": "http://s3.amazonaws.com/PLOT/"
},
"VALUE4": [
"SHOP",
"FABRIC",
"CITY",
"PLOT"
]
}
My code looks something like this: I honestly cant see how to manipulate the while to create the list for VALUES1 AND also how to differentiate when the loop reaches VALUE3, as this values have to be added into the distributions
public with sharing class ProcessorJSONController {
public String jsonStr { get; set; }
public List<Distribution> dist { get; set; }
public List<DistributionDetails> distDetails { get; set; }
public ProcessorJSONController() {
dist = new List<Distribution>();
distDetails = new List<DistributionDetails>();
//I HAVE MANUALLY MODIFY THIS VALUE BUT HERE WILL BE THE RESPONSE
jsonStr = '{"VALUE1":[{"PKT":"0.00","CODE":"1299600","COLOR":"GREEN","STATUS":"GOOD","WEIGHT":"6211","YEARS":"5","LOC":"SYD"},{"PKT":"0.01","CODE":"129400","COLOR":"RED","STATUS":"POOR","WEIGHT":"621","YEARS":"1","LOC":"SYD"}],"VALUE2":["PKT","CODE","COLOR","STATUS","WEIGHT","YEARS","LOC"],"VALUE3":{"SHOP":"https://s3.amazonaws.com/SHOP/","FABRIC":"https://s3.amazonaws.com/FABRIC/","CITY":"http://s3.amazonaws.com/CITY/","PLOT":"http://s3.amazonaws.com/PLOT/"},"VALUE4":["SHOP","FABRIC","CITY","PLOT"]}';
JSONParser parser = JSON.createParser(jsonStr);
String text = '';
while (parser.nextToken() != null) {
if(parser.getCurrentToken() == JSONToken.START_OBJECT){
parser.nextToken();
text = parser.getText() + ', ' + text;
}
//System.debug(parser.getText() + parser.nextToken() + ' - ' + parser.getText());
if (parser.getCurrentToken() == JSONToken.START_ARRAY) {
//parser.nextToken();
text = parser.getText() + ', ' + text;
}
}
}
public class Distribution {
public Double PKT { get; set; }
public String CODE { get; set; }
public String COLOR { get; set; }
public String STATUS { get; set; }
public String WEIGHT { get; set; }
public Integer YEARS { get; set; }
public String LOC { get; set; }
public String SHOP { get; set; }
public String FABRIC { get; set; }
public String CITY { get; set; }
public String PLOT { get; set; }
public Distribution (Double PKT, String CODE, String COLOR, String STATUS, String WEIGHT, Integer YEARS, String LOC, String SHOP, String FABRIC, String CITY, String PLOT){
this.PKT = PKT;
this.CODE = CODE;
this.COLOR = COLOR;
this.STATUS = STATUS;
this.WEIGHT = WEIGHT;
this.YEARS = YEARS;
this.LOC = LOC;
this.SHOP = SHOP;
this.FABRIC = FABRIC;
this.CITY = CITY;
this.PLOT = PLOT;
}
}
public class DistributionDetails{
public String SHOP { get; set; }
public String FABRIC { get; set; }
public String CITY { get; set; }
public String PLOT { get; set; }
public DistributionDetails (String SHOP, String FABRIC, String CITY, String PLOT){
this.SHOP = SHOP;
this.FABRIC = FABRIC;
this.CITY = CITY;
this.PLOT = PLOT;
}
}
}