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?

2
  • Delete comma after property2. (I deleted my answer to add this comment) Commented Aug 19, 2014 at 15:18
  • Comma removed. This was just a quick example I threw together to get the idea across! Commented Aug 19, 2014 at 16:04

2 Answers 2

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>.

0
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);
}

You must log in to answer this question.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.