+ Start a Discussion
java salesforcejava salesforce 

System.JSONException: Can not start an object, expecting field name

error at comment line. 

@RestResource(urlMapping='/RestWebservice1/*')
global class RestWebservice1 {
@HttpGet
    global static string doGet(){
       List<Account> accs = [select Name,Phone from Account limit 4];
        System.JSONGenerator jg = JSON.createGenerator(true);
        jg.writeStartObject();
        jg.writeFieldName('Status');
        if(accs.size()>0){
            jg.writeString('Success');
            jg.writeNumberField('count',accs.size());
            jg.writeFieldName('Accounts');
            for(Account a: accs){
                jg.writeStartObject();    // I'm getting error here 
                jg.writeStringField('Name', a.Name);
                jg.writeStringField('Phone', a.Phone);
                jg.writeEndObject();
            }
        }else{
            jg.writeString('Failed');
            jg.writeStringField('Reason', 'No Records Found');    
        }
        jg.writeEndObject();
            string result=jg.getAsString();
            System.debug('Json String :'+result);
        return result;
    } 
}
Aslam ChaudharyAslam Chaudhary
try this
List<Account> accs = [select Name,Phone from Account limit 4];
        System.JSONGenerator jg = JSON.createGenerator(true);
        jg.writeStartObject();
        jg.writeFieldName('Status');
        if(accs.size()>0){
            jg.writeString('Success');
            jg.writeNumberField('count',accs.size());
            jg.writeFieldName('Accounts');
            jg.writeObject(accs);
            //for(Account a: accs){
            //    jg.writeStartObject();    // I'm getting error here 
            //    jg.writeStringField('Name', a.Name);
                //jg.writeStringField('Phone', a.Phone);
            //    jg.writeEndObject();
            //}
        }else{
            jg.writeString('Failed');
            jg.writeStringField('Reason', 'No Records Found');    
        }
        jg.writeEndObject();
            string result=jg.getAsString();

Else let me what output you need.
java salesforcejava salesforce
Thanks for reply,
Bro I tried that but I need to display {"Accounts" : {"name":"Sample","Phone":"12345679"},{"name":"Sample1" , "phone": "1234567"}}  by using  JSONGenerator
Aslam ChaudharyAslam Chaudhary
Try this
List<Account> accs = [select Name,Phone from Account limit 4];
        System.JSONGenerator jg = JSON.createGenerator(true);
        jg.writeStartObject();
        jg.writeFieldName('Status');
        if(accs.size()>0){
            jg.writeString('Success');
            jg.writeNumberField('count',accs.size());
            jg.writeFieldName('Accounts');
            list<map<string,string>> listAcc=new list<map<string,string>> ();
            for(Account a: accs){
                Map<string,string> singleAcc=new map<string,string>();
                //jg.writeStartObject();    // I'm getting error here 
                //jg.writeStringField('Name', a.Name);
                //jg.writeStringField('Phone', a.Phone);
                //jg.writeEndObject();
                singleAcc.put('Name', a.Name);
                singleAcc.put('Phone', a.Phone);
                
                                listAcc.add(singleAcc);
            }
            jg.writeObject(listAcc);
        }else{
            jg.writeString('Failed');
            jg.writeStringField('Reason', 'No Records Found');    
        }
        jg.writeEndObject();
            string result=jg.getAsString();

 
java salesforcejava salesforce
Thanks bro for rly, but I need to display {"Accounts" : {"name":"Sample","Phone":"12345679","AnnualRevenue":300000},{"name":"Sample1" , "phone": "1234567","AnnualRevenue":300000}}  by using  JSONGenerator
The problem is we are using Map<string,string> which accepts only string content, But I need to display different datatype values as well like AnnualRevenue which is currency...
Aslam ChaudharyAslam Chaudhary
I have change the map of string , string to map of string , object 
 
List<Account> accs = [select Name,Phone,AnnualRevenue from Account limit 4];
        System.JSONGenerator jg = JSON.createGenerator(true);
        jg.writeStartObject();
        jg.writeFieldName('Status');
        if(accs.size()>0){
            jg.writeString('Success');
            jg.writeNumberField('count',accs.size());
            jg.writeFieldName('Accounts');
            list<map<string,object>> listAcc=new list<map<string,object>> ();
            for(Account a: accs){
                Map<string,object> singleAcc=new map<string,object>();
                singleAcc.put('Name', a.Name);
                singleAcc.put('AnnualRevenue',a.AnnualRevenue);
                singleAcc.put('Phone',a.phone);
                listAcc.add(singleAcc);
            }
            jg.writeObject(listAcc);
        }else{
            jg.writeString('Failed');
            jg.writeStringField('Reason', 'No Records Found');    
        }
        jg.writeEndObject();
            string result=jg.getAsString();

 
Arpit Jain92Arpit Jain92
@java Salesforce

Did yu get the solution for this?
I am also facing the same isuue.

Thanks,
Arpit