0

I need to deserialize an json where the token has special characters, but is not working as expected, it's not passing through the token and comparing to assign, am I forgetting something?

controller:

List<JSON2Apex> jsonlist = (List<JSON2Apex>) JSON.deserialize(base64Data, List<JSON2Apex>.class);

this following JSON:

[{  
      "Booking #":"3321234",
      "Parent Number":"412321",
      "Booking Date":"2017-10-04",
      "Departure Date":"2018-01-13"
   }]

I used the tool http://json2apex.herokuapp.com/ and it generated the following code:

public class JSON2Apex {

public String bookingNumber {get;set;} 
public String parentBooking {get;set;} 
public String bookingDate {get;set;} 
public String departureDate {get;set;}

public JSON2Apex(JSONParser parser) {
    while (parser.nextToken() != System.JSONToken.END_OBJECT) {
        if (parser.getCurrentToken() == System.JSONToken.FIELD_NAME) {
            String text = parser.getText();
            if (parser.nextToken() != System.JSONToken.VALUE_NULL) {
                if (text == 'Booking #') {
                    bookingNumber = parser.getText();
                } else if (text == 'Parent Booking') {
                    parentBooking = parser.getText();
                } else if (text == 'Booking Date') {
                    bookingDate = parser.getText();
                } else if (text == 'Departure Date') {
                    departureDate = parser.getText();
                } else {
                    System.debug(LoggingLevel.WARN, 'JSON2Apex consuming unrecognized property: '+text);
                    consumeObject(parser);
                }
            }
        }
    }
}


public static List<JSON2Apex> parse(String json) {
    System.JSONParser parser = System.JSON.createParser(json);
    return arrayOfJSON2Apex(parser);
}

public static void consumeObject(System.JSONParser parser) {
    Integer depth = 0;
    do {
        System.JSONToken curr = parser.getCurrentToken();
        if (curr == System.JSONToken.START_OBJECT || 
            curr == System.JSONToken.START_ARRAY) {
            depth++;
        } else if (curr == System.JSONToken.END_OBJECT ||
            curr == System.JSONToken.END_ARRAY) {
            depth--;
        }
    } while (depth > 0 && parser.nextToken() != null);
}

private static List<JSON2Apex> arrayOfJSON2Apex(System.JSONParser p) {
    List<JSON2Apex> res = new List<JSON2Apex>();
    if (p.getCurrentToken() == null) p.nextToken();
    while (p.nextToken() != System.JSONToken.END_ARRAY) {
        res.add(new JSON2Apex(p));
    }
    return res;
}

}

1
  • You might be better off just using a Map<String, Object> here and JSON.deserializeUntyped.
    – Adrian Larson
    Jul 26, 2018 at 15:17

1 Answer 1

0

This form of the generated code needs to be called rather than passed as a type to the JSON.deserialize method so in your controller:

List<JSON2Apex> jsonlist = JSON2Apex.parse(base64Data);

If your variable base64Data does contain encoded data, you will also need to apply EncodingUtil.base64Decode.

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .