I have a string object which I am trying to deserilize but when there is a json object in an another json object it has issues deserilizing.
[
{
"attributes": {
"type": "Account",
"url": "/services/data/77.0/object/Account/1234"
},
"Id": "1234",
"IsDeleted": false,
"MasterRecordId": null,
"Name": "Stevens Smith",
"LastName": null,
"FirstName": null,
"Salutation": null,
},
{
"attributes": {
"type": "Account",
"url": "/services/data/v77.0/object/Account/12345"
},
"Id": "12345",
"IsDeleted": false,
"MasterRecordId": null,
"Name": "Mr John Smith",
"LastName": null,
"FirstName": null,
"Salutation": null,
}
]
My Class is defined as follows:
public Account() {
public string? attributes;
public string? id;
public string? isDeleted;
public string? masterRecordId;
public string? name;
public string? lastName;
public string? firstName;
public string? salutation;
}
The error message I get is Unexpected character encountered while parsing value: {. Path ‘[0].attributes’, line 3, position 19.
3
Answers
You should have two classes
If you just want to save the
Attrubute
object as a string you could do something like this (syntax could be different depending if you’re using newtonsoft or not)You want don’t want to define the
Attrubute
class. You could make it dynamic object and if you want the string version – you could convert within your class. So something like thisA lot of your variables are the wrong type. It’s much safer to use the correct types – that way if you ever reserialize to JSON it will stay valid (for example isDeleted – even if you parse it in as a string, reserializing would store it as :"false" instead of :false).
If you do want to keep things as string, a better option is to leave it as json instead with JObject, and call .ToString() to return the json value when needed
if you need attributes as string you can do it using a counstructor for example, but before you will have to fix the class by adding getter/setter