skip to Main Content

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


  1. You should have two classes

    public class Account() {
        public Attrubute attributes;
        public string? id;
        public string? isDeleted;
        public string? masterRecordId;
        public string? name;
        public string? lastName;
        public string? firstName;
        public string? salutation;
    }
    
    public class Attrubute() {
        public string? type;
        public string? url;
    }
    
    

    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)

    public class Account() {
        public Attrubute attributes;
        public string? id;
        public string? isDeleted;
        public string? masterRecordId;
        public string? name;
        public string? lastName;
        public string? firstName;
        public string? salutation;
        public string attributesString => attributes != null ? JsonConvert.Serialize(attributes) : string.empty;
    }
    

    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 this

    public class Account {
        public string? id;
        public string? isDeleted;
        public string? masterRecordId;
        public string? name;
        public string? lastName;
        public string? firstName;
        public string? salutation;
        
        public dynamic attributes;
        public string Attributes => attributes != null ? JsonConvert.SerializeObject(attributes) : string.Empty;
    }
    
    Login or Signup to reply.
  2. A 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).

    public class Account
    {
        public Attrubute attributes;
        public string stringAttributes //if you need a string accessor to attributes you can just use a property
            {
                get {
                    return $"{attributes?.type}:{attributes?.url}";
                }
            }
        public string? id;
        public bool isDeleted;
        public string? masterRecordId;
        public string? name;
        public string? lastName;
        public string? firstName;
        public string? salutation;
    }
    
    public class Attrubute
    {
        public string? type;
        public string? url;
    }
    

    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

    public class Account
    {
        public JObject attributes;
        public string? id;
        public JObject isDeleted;
        public string? masterRecordId;
        public string? name;
        public string? lastName;
        public string? firstName;
        public string? salutation;
        public string GetAttributesJson() {
             return attributes.ToString();
        }
    }
    
    Login or Signup to reply.
  3. 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

    List<Account> accounts = JsonConvert.DeserializeObject<List<Account>>(json);
    
    public class Account
    {
        public int? id { get; set; }
        public string? attributes { get; set; }
        //.... another properties
        [JsonConstructor]
        public Account(JObject attributes)
        {
            this.attributes = string.Join(",", attributes.Properties());
    
           //or a json string
           this.attributes =  attributes.ToString();
    
          // or a url
           this.attributes = (string)attributes["url"]
    
        }
    }
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search