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

时间:2018-08-20 06:30:09

标签: asp.net-mvc asp.net-web-api json.net

我遇到此异常:

  

Newtonsoft.Json.JsonReaderException         HResult = 0x80131500         消息=解析值{时遇到意外字符。路径“ outputObject.address”,第17行,位置16。

从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();
        }
    }

Foowing是输出对象接口   OutputObject接口

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         消息=解析值{时遇到意外字符。路径“ outputObject.address”,第17行,位置16。         资料来源= Newtonsoft.Json         堆栈跟踪:          在Newtonsoft.Json.JsonTextReader.ReadStringValue(ReadType readType)          在Newtonsoft.Json.JsonTextReader.ReadAsString()          在Newtonsoft.Json.JsonReader.ReadForType(JsonContract合同,布尔hasConverter)          在Newtonsoft.Json.Serialization.JsonSerializerInternalReader.PopulateObject(Object newObject,JsonReader reader,JsonObjectContract contract,JsonProperty member,String id)中          在Newtonsoft.Json.Serialization.JsonSerializerInternalReader.CreateObject(JsonReader阅读器,Type objectType,JsonContract合同,JsonProperty成员,JsonContainerContract containerContract,JsonProperty containerMember,对象existValue)中          在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阅读器,Type objectType,JsonContract合同,JsonProperty成员,JsonContainerContract containerContract,JsonProperty containerMember,对象existValue)中          在Newtonsoft.Json.Serialization.JsonSerializerInternalReader.Deserialize(JsonReader reader,Type objectType,Boolean checkAdditionalContent)中          在Newtonsoft.Json.JsonSerializer.DeserializeInternal(JsonReader reader,Type objectType)          在Newtonsoft.Json.JsonConvert.DeserializeObject(字符串值,类型,JsonSerializerSettings设置)

1 个答案:

答案 0 :(得分:3)

您的问题是您已将CarLookupOutputObject.Address声明为string,但是相应的JSON值是一个对象:

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

如其Serialization Guide中所述,只有原始.Net类型和可转换为string的类型才被序列化为JSON字符串。由于"address"的值不是原始值,因此将引发异常。

相反,按照http://json2csharp.com/的建议,如下修改数据模型:

public interface ICarLookupOutputObject 
{
    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; }
}