4

I'm running a REST API which is returning the below:

"id" : "fdgsdgsdsdgsdg",
   "pageevents" : [ {
       "name" : "test",
       "number" : "0123442",
       "homepage" : "www.test.com"
    } ],
    "goals" : [ {
       "name" : "test",
       "number" : "0123442",
       "homepage" : "www.test.com"
    } ]
} ]

Im using the below code to de-serialize it, so i can display it as a pageblocktable on a visualforce page however im getting the error :

System.JSONException: Illegal value for primitive

public List<maMoments> getMomentsActivity() {
    Id contactId;
    if (contactId == null) {
        contactId = '0030O00001sqXJr';
    }

    String contactIdString = String.valueOf(contactId);
    String jsonResponse = this.AutomationDetailsMoments(contactIdString);
    system.debug(jsonResponse);
    List<maMoments> mAutomation = (List<maMoments>) JSON.deserialize(jsonResponse, List<maMoments>.class);
    system.debug(mAutomation);
    for(maMoments emailStructure : mAutomation){

    }
     return mAutomation;
}

public string AutomationDetailsMoments(String contactId) {
    contactId = '0030O00001sqXJr';
    Http h = new Http();
    HttpRequest req = new HttpRequest();
    req.setEndpoint('https://virtserver.swaggerhub.com/mickjdavies/Example/1.0.0/interestingmoments');
    req.setMethod('POST');
    req.setBody(contactId);

    // Send the request, and return a response
    HttpResponse res = h.send(req);
    system.debug(res.getBody());
    return res.getBody();
}

private class maMoments {
    public String contactId;
    public String id;
    public List<String> goals;
    public String name;
    public String homepage;
    public String phone;
}
2
  • 1
    From the documentation. Json.deserialize() only accepts apex types. You should try with JSON.deserializeUntyped(jsonString). Commented Jun 29, 2017 at 16:01
  • You can't really use the strict deserialization approach because number is a reserved keyword.
    – Adrian Larson
    Commented Jun 29, 2017 at 16:20

1 Answer 1

8

The issue is that is causing the immediate error is that goals is not a valid List<String> as it is a json object in your case. You also have a potential issue if you need to access the number property as number is a reserved keyword.

Two things to resolve:

  1. Adding a class for goals will resolve your immediate issue.
  2. Replacing number with number_Z in the JSON string to deserialize Properly, if you need the number property to be available

Without needing the number property

public with sharing class mytestClass {
    public static String tmp = '[{"id" : "0011400001Z4xuEAAR", "pageevents" : [ { "name" : "test", "number" : "0123442", "homepage" : "www.test.com" } ], "goals" : [ { "name" : "test", "number" : "0123442", "homepage" : "www.test.com" } ] } ]';

    public static maMoments[] doIt() {
        return (maMoments[]) json.deserialize(tmp, List<maMoments>.class);
    }

    public class maMoments {
        public String contactId;
        public String id;
        public List<goals> goals;
        public String name;
        public String homepage;
        public String phone;
    }

    public class goals{
        public string name;
        public string homepage;
    }

}

14:14:00.50 (89310700)|USER_DEBUG|[1]|DEBUG|(maMoments:[contactId=null, goals=(goals:[homepage=www.test.com, name=test]), homepage=null, id=0011400001Z4xuEAAR, name=null, phone=null])

If you need the number property

Replacing should be ok when you only have one reserved keyword.

public with sharing class mytestClass {

    public static String tmp = '[{"id" : "0011400001Z4xuEAAR", "pageevents" : [ { "name" : "test", "number" : "0123442", "homepage" : "www.test.com" } ], "goals" : [ { "name" : "test", "number" : "0123442", "homepage" : "www.test.com" } ] } ]';

    public static maMoments[] doIt() {
        return (maMoments[]) json.deserialize(tmp.replace('"number"','"number_Z"'), List<maMoments>.class);
    }

    public class maMoments {
        public String contactId;
        public String id;
        public List<goals> goals;
        public String name;
        public String homepage;
        public String phone;
    }

    public class goals{
        public string name;
        public string homepage;
        public string number_Z;
    }

}

14:17:09.44 (62331981)|USER_DEBUG|[1]|DEBUG|(maMoments:[contactId=null, goals=(goals:[homepage=www.test.com, name=test, number_Z=0123442]), homepage=null, id=0011400001Z4xuEAAR, name=null, phone=null])

1
  • Thank you for explaining this, didn't realise i needed to create a separate class for the object, got this displaying correctly now thank you Commented Jun 30, 2017 at 9:58

You must log in to answer this question.

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