2

I have a List of Schema.SObjectfield and I want to convert it to String.

I am getting below error.

System.JSONException: Apex Type unsupported in JSON: Schema.SObjectField

Here is my approach.

  public string jsondata{get;set;}




  List<Schema.SObjectField> fieldschema = new List<Schema.SObjectField>();

    for(Schema.SObjectField sfield : fieldMap.Values()) {
       fieldschema.add(sfield);
    }

     jsondata = JSON.serialize(fieldschema);
 jsonstring = (List<jsonvalue>) System.JSON.deserialize(jsondata , List<jsonvalue>.class);

can someone please let me know where is wrong in my approach or how to handle it?

3

You can't serialize SObjectField, as the error says, but it's easy to get the value as a String:

public string jsondata{get;set;}
List<String> fieldschema = new List<String>();

for(Schema.SObjectField sfield : fieldMap.Values()) {
   fieldschema.add(''+sfield);
}

jsondata = JSON.serialize(fieldschema);
  • I am getting this error with this code after execution in runtime. System.JSONException: Expected ABV_GenerateXMLs.jsonvalue but found "Id" at [line:1, column:2] Error is in expression '{!getFieldsPicklist}' in page generatexmls: Class.System.JSON.deserialize: line 15, column 1 – Testing_SFDC Jun 16 '18 at 15:16

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.