2

EDIT 2: Apparently Apex has no idea what escaping character is... after I removed manually all '\' from that JSON, it worked.

I know this question was asked tens of times, but so far nothing works for me. I have a simple JSON from external application like this one:

"[{\"Birthdate\":\"2004-11-11\",\"City1\":\"city1\",\"City2\":\"city22\",\"Country1\":\"bg\",\"Country2\":\"BG2\",\"Description\":\"a random desc\",\"Email\":\"jjrefjerf@efe.bg\",\"Fax\":\"32424234242\",\"FirstName\":\"fir333stname\",\"HomePhone\":\"82937423\",\"LastName\":\"rggrdgr\",\"Phone\":\"4234234242\",\"PostalCode\":\"34233\",\"PostalCode2\":\"231\",\"State1\":\"gkrdg\",\"State2\":\"state2\",\"Title\":\"a very frustrated SF user\",\"MailingStreet\":\"fsdfsdfs\",\"OtherStreet\":\"fdjsnfkjsd\"},{\"Birthdate\":\"2004-11-11\",\"City1\":\"city1\",\"City2\":\"city22\",\"Country1\":\"bg\",\"Country2\":\"BG2\",\"Description\":\"a random desc\",\"Email\":\"jjrefjerf@efe.bg\",\"Fax\":\"32424234242\",\"FirstName\":\"firs444tname\",\"HomePhone\":\"82937423\",\"LastName\":\"rggrdgr\",\"Phone\":\"4234234242\",\"PostalCode\":\"34233\",\"PostalCode2\":\"231\",\"State1\":\"gkrdg\",\"State2\":\"state2\",\"Title\":\"a very frustrated SF user\",\"MailingStreet\":\"fsdfsdfs\",\"OtherStreet\":\"fdjsnfkjsd\"},{\"Birthdate\":\"2004-11-11\",\"City1\":\"city1\",\"City2\":\"city22\",\"Country1\":\"bg\",\"Country2\":\"BG2\",\"Description\":\"a random desc\",\"Email\":\"jjrefjerf@efe.bg\",\"Fax\":\"32424234242\",\"FirstName\":\"firstn555ame\",\"HomePhone\":\"82937423\",\"LastName\":\"rggrdgr\",\"Phone\":\"4234234242\",\"PostalCode\":\"34233\",\"PostalCode2\":\"231\",\"State1\":\"gkrdg\",\"State2\":\"state2\",\"Title\":\"a very frustrated SF user\",\"MailingStreet\":\"fsdfsdfs\",\"OtherStreet\":\"fdjsnfkjsd\"}]"`

And i am trying to deserialize it using: List<JSON2Apex> obj = System.JSON.deserialize(json, List<JSON2Apex>.class); and my Json2Apex class look like this:

public class JSON2Apex {
    public String Birthdate;
    public String City1;
    public String City2;
    public String Country1;
    public String Country2;
    public String Description;
    public String Email;
    public String Fax;
    public String FirstName;
    public String HomePhone;
    public String LastName;
    public String Phone;
    public String PostalCode;
    public String PostalCode2;
    public String State1;
    public String State2;
    public String Title;
    public String MailingStreet;
    public String OtherStreet;
}

Is is possible for the error to comes from this string coming from C# service and therefore being put inside "" quotes, instead of single one ''? And if so, how can I change that because splitting it to chars and remaking it with '' instead doesn't work.

String source = res.getBody();
    String[] chars = source.split('');
    String newJson = '';
    for (Integer i = 1; i<chars.size();i++) {
        newJson = newJson + chars[i];
    }

The information there must be added to Contacts standard object, but I can't even extract it from that JSON.

P.S. - suggested answer didnt work.

  • according to the error message, I would assume that you will need to loop over your list to fill in the map – Novarg Sep 19 '17 at 14:45
  • The data you have doesn't look like a map - try creating a map with the element type you are looking for, and serialize it, then compare it to the json you currently have. – battery.cord Sep 19 '17 at 14:55
  • You have totally changed your initial question, making my answer as invalid, better post your initial question here, close it and raise a new question, otherwise members will get confused why did they voted my answer which was acceptable. – Santanu Boral Sep 20 '17 at 14:19
11

Invalid conversion from runtime type List to Map

So it is expecting a List which you are typecasting to a Map that's the issue. The thing that makes it a List is the fact that the JSON String starts with [ and ends with ]. Those delimiters are used to indicate a List (or Array if you prefer) in JSON.

I think you need to deserialize as below:

String str = response.getBody();

List<Contact> lstContact = (List<Contact>) JSON.deserialize(str, List<Contact>.class);

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.