代码之家  ›  专栏  ›  技术社区  ›  Praveen Rao Chavan.G

分析API响应时遇到意外字符

  •  2
  • Praveen Rao Chavan.G  · 技术社区  · 6 年前

    我有个例外:

    HResult=0x80131500

    从API反序列化响应数据时。 (帖子结尾处有完整的例外)

    代码

    return JsonConvert.DeserializeObject(webResponseEntity.ResponseData, typeof(CarLookupResponse)) as CarLookupResponse;
    

    模型

       public class CarLookupResponse : ICarLookupResponse
        {
            public ICarLookupResult Result { get; set; }
    
            public ICarLookupOutputObject OutputObject { get; set; }
    
            public CarLookupResponse()
            {
                Result = new CarLookupResult();
                OutputObject = new CarLookupOutputObject();
            }
        }
    

    输出对象接口

    public interface ICarLookupOutputObject 
        {
            int  CarId { get; set; }
    
            string CartestId { get; set; }
    
            int[] ModelYears { get; set; }
    
            string FirstName { get; set; }
    
            string LastName { get; set; }
    
            string Email { get; set; }
    
            string SSN { get; set; }
    
            string Address { get; set; }
        }
    

    JSON格式

       {
         "result": {
            "id": 1,
            "value": "lookup successful.",
            "error": null
          },
          "outputObject": {
            "CarId": 2025,
            "CartestId": "testing-02",
            "ModelYears": [
              2017,
              2018
            ],
            "firstName": "Troy",
            "lastName": "Aaster",
            "email": "testuser@gmail.com",
            "address": {
              "apartment": "",
              "state": "CA",
              "city": "BRISBANE",
              "zipCode": "94005",
              "streetAddress": "785, SPITZ BLVD"
            },
            "ssn": "511-04-6666"
          }
        }
    

    JSON格式 是有效的,我已经检查过了。

    Newtonsoft.Json.JsonReaderException HResult=0x80131500 源=Newtonsoft.Json 堆栈跟踪: 在Newtonsoft.Json.JsonTextReader.ReadStringValue(ReadType ReadType) 在Newtonsoft.Json.JsonTextReader.ReadAsString() 在Newtonsoft.Json.JsonReader.ReadForType(Json contract契约,布尔hascoverter) 在Newtonsoft.Json.Serialization.JsonSerializerInternalReader.PopulateObject(Object newObject,JsonReader reader,JsonObjectContract contract,JsonProperty member,String id) 在Newtonsoft.Json.Serialization.JsonSerializerInternalReader.CreateObject(JsonReader读取器,类型objectType,JsonContract协定,JsonProperty成员,JsonContainerContract containerContract,JsonProperty containerMember,对象existingValue) 在Newtonsoft.Json.Serialization.JsonSerializerInternalReader.SetPropertyValue上(JsonProperty属性、JsonConverter属性Converter、JsonContainerContract containerContract、JsonProperty containerProperty、JsonReader读取器、对象目标) 在Newtonsoft.Json.Serialization.JsonSerializerInternalReader.PopulateObject(Object newObject,JsonReader reader,JsonObjectContract contract,JsonProperty member,String id) 在Newtonsoft.Json.Serialization.JsonSerializerInternalReader.CreateObject(JsonReader读取器,类型objectType,JsonContract协定,JsonProperty成员,JsonContainerContract containerContract,JsonProperty containerMember,对象existingValue) 在Newtonsoft.Json.Serialization.JsonSerializerInternalReader.Deserialize(Json reader reader,Type objectType,Boolean checkAdditionalContent) 在Newtonsoft.Json.JsonSerializer.DeserializeInternal(Json reader reader,键入objectType) 在Newtonsoft.Json.JsonConvert.DeserializeObject(字符串值、类型类型、JsonSerializerSettings设置)

    1 回复  |  直到 6 年前
        1
  •  7
  •   Marcus Höglund    5 年前

    你的问题是你已经宣布 CarLookupOutputObject.Address 成为 string

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

    如its中所述 Serialization Guide ,只有基元.Net类型和可转换为 一串 "address" 不是基元的,则引发异常。

    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; }
    }