You need to sign in to do that
Don't have an account?
java 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;
}
}
@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;
}
}
Else let me what output you need.
Bro I tried that but I need to display {"Accounts" : {"name":"Sample","Phone":"12345679"},{"name":"Sample1" , "phone": "1234567"}} 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...
Did yu get the solution for this?
I am also facing the same isuue.
Thanks,
Arpit