2

I am having issues with JSON parsing. Here is my controller method:

    @RemoteAction
    global static String GetDetails(string sName)
    {
        //do soamething with sName later        
        List<String> sectorList = new List<String>();
        sectorList.add('Healthcare');
        sectorList.add('Healthcare');
        sectorList.add('Healthcare');
        return JSON.serialize(sectorList);
    }

& here is my JavaScript:

<script type="text/javascript">
    function getRemoteDeals(sectorName) {
        Visualforce.remoting.Manager.invokeAction(
            '{!$RemoteAction.MyController.GetDetails}',
            sectorName,
            function(result, event){
            if (event.status) {
                    //do something it is a success                      
                    alert(result);                      
                    var obj = JSON.parse(result);                        
                    alert(obj);
                } else if (event.type === 'exception') {
                    document.getElementById("responseErrors").innerHTML = 
                        event.message + "<br/>\n<pre>" + event.where + "</pre>";
                } else {
                    document.getElementById("responseErrors").innerHTML = event.message;
                }
            }, 
            {escape: true}
        );
    }
</script>

Firebug error: SyntaxError: JSON.parse: unexpected character at line 1 column 2 of the JSON data

alert(result) throws this --> ["Healthcare","Healthcare","Healthcare"]

Also alert image attached:

Please see image attached of the alert

  • Try logging the result and add that to your question. – Adrian Larson Nov 12 '15 at 19:25
  • Please provide what values you get in result. – the_phantom Nov 12 '15 at 19:28
  • added result as an alert & image – user569925 Nov 12 '15 at 19:35
  • what is the expected behaviour you are looking for ? – Himanshu Nov 12 '15 at 19:36
  • above code is not executing as expected. In firebug I get following error: SyntaxError: JSON.parse: unexpected character at line 1 column 2 of the JSON data – user569925 Nov 12 '15 at 19:37
4

You have simpler method to do this.

Remote action method:

@RemoteAction
global static List<String> GetDetails(string sName)
{
    //do soamething with sName later        
    List<String> sectorList = new List<String>();
    sectorList.add('Healthcare');
    sectorList.add('Healthcare1');
    sectorList.add('Healthcare2');
    return sectorList;
}

There is no need to put JSON.parse because Salesforce return json only in following was so your

JS Method

// var obj = JSON.parse(result);    
alert(result[0]); //will print first value
alert(result[1]); //will print first value
alert(result[2]); //will print first value
  • Hi Himanshu, your above answer works if I have a list of string. If I use the above code and replace it with List<MyCustomObject__c> then my alerts are [object Object] irrespective of the object type. Any insights on that? – user569925 Nov 12 '15 at 19:55
  • that is correct. – Himanshu Nov 12 '15 at 19:57
  • is there a way to retrieve object value? – user569925 Nov 12 '15 at 19:58
  • 1
    it is simple json object parsing. you can use . notation to access the value. – Himanshu Nov 12 '15 at 19:58

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.