0

in my java application, I'm reading a JSON array from a REST API.

Here is my code, which reads individual objects from a JSON array, and attempts to convert each JSON object to a strongly-types one:

JSONArray jsonArray = new JSONArray(videoFromMessage);

                for (int i = 0; i < jsonArray.length(); i++) {                  
                    JSONObject custtableObject = jsonArray.getJSONObject(i);

                    ObjectMapper mapper = new ObjectMapper();

                    Custtable custTable = mapper.readValue(custtableObject.toString(), new TypeReference<Custtable >(){});

                        }

Each custTable object in that array contains a field named "modifieddatetime", with a value resembling this:

Fri Dec 31 18:00:00 CST 1

The readValue call throws this exception:

    com.fasterxml.jackson.databind.exc.InvalidFormatException: 
    Can not construct instance of java.util.Date from String value 'Fri Dec 31 18:00:00 CST 1': 
not a valid representation (error: Failed to parse Date value 'Fri Dec 31 18:00:00 CST 1': 
Can not parse date "Fri Dec 31 18:00:00 CST 1":
 not compatible with any of standard forms 
("yyyy-MM-dd'T'HH:mm:ss.SSSZ", "yyyy-MM-dd'T'HH:mm:ss.SSS'Z'", "EEE, dd MMM yyyy HH:mm:ss zzz", "yyyy-MM-dd"))

Is there some method, function, utility, or an API, which I could call, to convert this field value to an acceptable format?

Eugene Goldberg
  • 14,286
  • 20
  • 94
  • 167
  • Why are you using two JSON parsing libraries? Why are you creating a new `ObjectMapper` instance in each iteration? Why are you using a `TypeReference` for just a non parameterized type? – Sotirios Delimanolis Sep 23 '16 at 16:49
  • `ObjectMapper` can parse JSON arrays just fine. You'd use a `TypeReference` in this case to represent a `List` or use a `Custtable[].class` directly. – Sotirios Delimanolis Sep 23 '16 at 16:49
  • `ObjectMapper` is a heavy weight object. You don't need to create a new one every time. It doesn't mutate its state based on things it parses. It's also thread safe. – Sotirios Delimanolis Sep 23 '16 at 16:53

0 Answers0