Unexpected character encountered while parsing API response

前端 未结 1 1088
庸人自扰
庸人自扰 2020-12-19 22:20

I am getting this exception:

Newtonsoft.Json.JsonReaderException HResult=0x80131500 Message=Unexpected character encountered while par

1条回答
  •  情深已故
    2020-12-19 22:57

    Your problem is that you have declared CarLookupOutputObject.Address to be a string, but the corresponding JSON value is an object:

    "address": {
      "apartment": "",
      ...
    },
    

    As explained in its Serialization Guide, only primitive .Net types and types convertible to string are serialized as JSON strings. Since the value of "address" is not primitive, the exception is thrown.

    Instead, modify your data model as follows, as suggested by http://json2csharp.com/:

    public class CarLookupOutputObject 
    {
        public Address address { get; set; }
    
        // Remainder unchanged
    }
    
    public class Address
    {
        public string apartment { get; set; }
        public string state { get; set; }
        public string city { get; set; }
        public string zipCode { get; set; }
        public string streetAddress { get; set; }
    }
    

    0 讨论(0)
提交回复
热议问题