3

I'm having so trouble deserializing a JSON stream that contains an array as the root:

[
    {
        "object": {
                     "property1":"000000",
                     "property2":"000000"
                  }
    },
    {
        "object": {
                     "property1":"000000",
                     "property2":"000000"
                  }
    }
]

I tried using JSON.deserializeUntyped and assigning to a Map, but I keep getting a "invalid conversion runtime type LIST to MAP". This method worked fine when I was dealing with single JSON objects, but loading multiples into an array trips this error. Should I be using a different type for deserialization?

  • Delete comma after property2. (I deleted my answer to add this comment) – SF_user Aug 19 '14 at 15:18
  • Comma removed. This was just a quick example I threw together to get the idea across! – Lee Harrison Aug 19 '14 at 16:04
4

You have to cast it into a List<Object>, and for each element in the list, you can access the values as a Map<Object,Object>.

2

As well as containing the extra commas, the JSON you show is an array of objects. You have to type convert it in two steps:

String s = '[{"object": {"property1":"000000","property2":"000000"}},'
        + '{"object": {"property1":"000000","property2":"000000"}}]';

List<Object> l = (List<Object>) JSON.deserializeUntyped (s);
for (Object o : l) {
    Map<String, Object> m = (Map<String, Object>) o;
    System.debug('>>> m=' + m);
}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.