0

i am tring to insert list of account records using dto class...

list<dtoaccount> a = (list<dtoaccount>)JSON.deserialize(body.toString(),dtoaccount.class);

i used this code for converting json body here "dtoaccount" is Wrapperclass but i am getting error:

"errorCode": "APEX_ERROR", "message": "System.TypeException: Invalid conversion from runtime type accountinsertion.dtoaccount to List\n\nClass.accountinsertion.postaccount: line 11, column 1"

wrapper class:

public class dtoaccount{
public string name{get;set;}
public string Phone{get;set;}
public string type{get;set;}
public string Industry{get;set;}

public dtoaccount(string name,string Phone,string type,string Industry){
this.Name = Name;
this.Phone = Phone;
this.type = type;
this.Industry = Industry;
}
}

body:

{
"name":"xyz",
"Phone":"123456789",
  "type" : "prospect",
  "Industry" : "banking"
  }
2
  • Can you pls add body.toString() and dtoaccount ? so we can compare what is wrong.
    – Ratan Paul
    Mar 9, 2016 at 10:51
  • Thank you for responding can you please check updated code
    – user28833
    Mar 9, 2016 at 10:54

1 Answer 1

2

If the JSON really is a list/array (starts with [) the code would need to be:

List<dtoaccount> a = (List<dtoaccount>) JSON.deserialize(
        body.toString(),
        List<dtoaccount>.class
        );

and if the JSON is a single object (starts with {) the code would need to be:

dtoaccount a = (dtoaccount) JSON.deserialize(
        body.toString(),
        dtoaccount.class
        );

PS

Based on your update to the question it should be the latter.

6
  • i used this code even i m getting error:"errorCode": "APEX_ERROR", "message": "System.JSONException: Malformed JSON: Expected '[' at the beginning of List/Set\n\nClass.System.JSON.deserialize: line 15, column 1\nClass.accountinsertion.postaccount: line 11, column 1"
    – user28833
    Mar 9, 2016 at 11:05
  • @user123 Using the second piece of code or the first?
    – Keith C
    Mar 9, 2016 at 11:06
  • i need to insert list of account records i used first piece of code only
    – user28833
    Mar 9, 2016 at 11:11
  • @user123 Your JSON appears to be a single item not a list of those items. Change the code that generates the JSON to be working with a list and then you can use the first first piece of code.
    – Keith C
    Mar 9, 2016 at 11:19
  • could you please tell me example code for list of records in json formate in body
    – user28833
    Mar 9, 2016 at 12:05

You must log in to answer this question.