1

I have a JSON string which looks like this:

jsonstring = {"XX":["Blah blah"]}

I need to get the 'Blah blah' into a string variable. How do I do this?

Tried doing it this way, but all that returned was '{', so that's not working.

string result = (String)JSON.deserialize(jsonstring, String.Class);

1 Answer 1

1

Use the JSON.deserializeUntyped to get an Object and you can typecast it further into a Map .

You can then easily start reading the object with key and typecasting it further.

String jsonstring = '{"XX":["Blah blah"]}';

Map<String, Object> m = (Map<String, Object>)JSON.deserializeUntyped(jsonstring);

System.debug((List<Object>)m.get('XX'));

for(Object o : (List<Object>)m.get('XX')) {
   system.debug((String) o);
}
2
  • I tried that as well, but then I get this error: Invalid conversion from runtime type List<ANY> to String. Why would that be?
    – Irene
    Commented Apr 9, 2019 at 3:46
  • I just fixed it . Sounds like it is array of string and hence you need an array . Check it out . Commented Apr 9, 2019 at 3:48

You must log in to answer this question.

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