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

5
  • Try logging the result and add that to your question.
    – Adrian Larson
    Commented Nov 12, 2015 at 19:25
  • Please provide what values you get in result. Commented Nov 12, 2015 at 19:28
  • added result as an alert & image
    – user569925
    Commented Nov 12, 2015 at 19:35
  • what is the expected behaviour you are looking for ?
    – Himanshu
    Commented Nov 12, 2015 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
    Commented Nov 12, 2015 at 19:37

1 Answer 1

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
4
  • 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
    Commented Nov 12, 2015 at 19:55
  • that is correct.
    – Himanshu
    Commented Nov 12, 2015 at 19:57
  • is there a way to retrieve object value?
    – user569925
    Commented Nov 12, 2015 at 19:58
  • 1
    it is simple json object parsing. you can use . notation to access the value.
    – Himanshu
    Commented Nov 12, 2015 at 19:58

You must log in to answer this question.

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