locked
deserialize a nested Json, RRS feed

  • Question

  • User450763225 posted

    I was trying to deserialize nested json, however I got the below error, please advise,

    Exception: Newtonsoft.Json.JsonReaderException: After parsing a value an unexpected character was encountered: {. Path 'ShppingAddress[0]', line 14, position 2.

     static void Main(string[] args)
             {
                 string json = @"{
      'firstname':'test',
      'lastname':'test',
        
      'ShppingAddress':
      [
      {
      'ont_addressline1':'1',
      'ont_addressline2':'',
      'ont_city':'1',
      'ont_province':'',
      'ont_postalcode':''
      }
      {
      'ont_addressline1':'2',
      'ont_addressline2':'',
      'ont_city':'2',
      'ont_province':'',
      'ont_postalcode':''
      }
      ],
        
     }";
                 Console.WriteLine(json);
                 ShippingInfo shippingInfo = JsonConvert.DeserializeObject<ShippingInfo>(json);
                 Console.WriteLine("success");
             }
        
             public class ShippingInfo
             {
                 public string firstname { get; set; }
                 public string lastname { get; set; }
                 public address address { get; set; }
             }
        
             public class address
             {
                 public string ont_addressline1 { get; set; }
                 public string ont_addressline2 { get; set; }
        
                 public string ont_city { get; set; }
                 public string ont_province { get; set; }
                 public string ont_postalcode { get; set; }
             }
         }

    Monday, April 26, 2021 2:19 AM

All replies

  • User303363814 posted

    Line 14 is the brace that starts the second element of the 'ShppingAddress' array.  You do not have a separator between the two elements of the array.

    That is line 13 of your json should be 

    },

    Monday, April 26, 2021 4:20 AM
  • User-939850651 posted

    Hi sdnd2000,

    It seems that you are not very familiar with the structure of Json, Json uses commas to separate the attributes in the object and the objects in the collection.

    In your case, Json should look like this:

    string json = @"{
                      'firstname':'test', 
    'lastname':'test',
    'ShppingAddress':[
    {
    'ont_addressline1':'1',
    'ont_addressline2':'',
    'ont_city':'1',
    'ont_province':'',
    'ont_postalcode':''
    },
    {
    'ont_addressline1':'2',
    'ont_addressline2':'',
    'ont_city':'2',
    'ont_province':'',
    'ont_postalcode':''
    }
    ]
    }";

    You could also read this article, it has clearer examples, I think it will be helpful to you.

    Although the constructor allows trailing commas in Json in C#, but I recommand that you follow the Json specification to write code.

    Best regards,

    Xudong Peng

    Tuesday, April 27, 2021 2:51 AM
  • User-474980206 posted

    Also double quotes are required, single quotes are not supported.

    Tuesday, April 27, 2021 3:07 PM