1

I have getting the below JSON on using JSON.serialize

[ {
  "lname" : "Singh",
  "hcpZipCode" : null,
  "hcpState" : null,
  "hcpEmail" : "test@test.com",
  "hcpCity" : null,
  "hcpAddLine1" : null,
  "fname" : "Abhey",
  "countryCode" : "AUS"
} ]

I don't know why this '[' is coming at the begining.I don't need this '[' at the begining due to which I am getting System.JSONException: Malformed JSON: Expected '{' at the beginning of object

code

   public PageReference step2() {

       string JSONstr='';
       List<HCPRequest> conWrapList = new List<HCPRequest>();
       HCPRequest tempcontactWrapper = new HCPRequest();
       tempcontactWrapper.fname='Abhey';
       tempcontactWrapper.lname='Singh';
       tempcontactWrapper.countryCode='AUS';
       tempcontactWrapper.hcpEmail=contact.Email;
        conWrapList.add(tempcontactWrapper);

        JSONstr = JSON.serializePretty(conWrapList);
        System.debug('\n******'+JSONstr +'\n******');

        String clientId = 'xy';
        String clientSecret = 'x';

        //We can also store our username password in custom setting.
        String username='x';//salesforce username 
        String password='x';//password+securitytoken
        // Generating the Access Token
        HttpRequest req = new HttpRequest();
        req.setMethod('POST');
        req.setEndpoint('https://test.salesforce.com/services/oauth2/token');// this is the OAuth endpoint where this request will be hit
        req.setBody('grant_type=password&client_id='+clientId+'&client_secret='+clientSecret+'&username='+username+'&password='+password);

        Http http = new Http();
        HTTPResponse res = http.send(req);
        String str = res.getBody();
        wrapObj = (Wrapper)Json.deserialize(str,Wrapper.class);
        accessToken = wrapObj.access_token;
        instanceUrl = wrapObj.instance_url;

        HttpRequest req1 = new HttpRequest();
        string response='';
        req1.setMethod('POST');
        //req.setEndpoint(wrapObj.instance_url+'/services/apexrest/Account/getAccountById?name=champaKAli');
        string EndPt = 'y';
        string email='test@test.com';
        req1.setEndpoint(EndPt);
        req1.setHeader('Authorization', 'OAuth '+wrapObj.access_token);
         req1.setHeader('Content-Type', 'application/json');
         //req1.setBody('{"request":{"lname" : "Singh","hcpZipCode" : null,"hcpState" : null,"hcpEmail" : "test@test.com","hcpCity" : null,"hcpAddLine1" : null,"fname" : "Abhey","countryCode" : "AUS"}}');
         req1.setBody('{"request":'+JSON.Serialize(conWrapList)+'}');
         //req.setBody('{"request": {"countryCode": "AUS","hcpEmail" : ""+email}}');

        //req.setTimeout(60000);



         //req.setHeader('Content-Length', '0');
        Http http1 = new Http();
        HTTPResponse res1 = http1.send(req1);

        System.debug('***Response***** ' + res1.getBody());
        if(res1.getBody()!=null)
        {
            response=res1.getBody();
            HCPResponse saj=(HCPResponse)JSON.deserialize(response,HCPResponse.class);
          System.debug('***Response came full***** ' + saj);
           System.debug('***Response came***** ' + saj.accList);

           for(HCPResponseDetail s :saj.accList)
           {
           System.debug('***Response email***** ' + s.hcpEmail);
           System.debug('***Response add2***** ' + s.hcpAddrLine2);
           contact.Address1_Line2__c=s.hcpAddrLine2;
           }


        //return response;
        //string methodcall=methodGet(JSONstr);


            return Page.opptyStep2CustomReg;
        }
2

You asked it to serialize a list, which is going to produce an array in json.

List<HCPRequest> conWrapList = new List<HCPRequest>();
JSONstr = JSON.serializePretty(conWrapList);       

If you don't want a json array, just pass serialize an object instead of a list, e.g.

JSONstr = JSON.serializePretty(tempcontactWrapper);
2

It is because the system returning the response is returning a List of Object so you will need to adjust your code to handle a list similar to:

HCPResponse[] saj=(List<HCPResponse>)JSON.deserialize(response,List<HCPResponse>.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.