解析API响应时遇到意外字符 [英] Unexpected character encountered while parsing API response

查看:144
本文介绍了解析API响应时遇到意外字符的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我遇到此异常:

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

Newtonsoft.Json.JsonReaderException HResult=0x80131500 Message=Unexpected character encountered while parsing value: {. Path 'outputObject.address', line 17, position 16.

从API反序列化响应数据时. (帖子末尾完全例外)

when deserializing the response data from an API. (complete exception on the end of the post)

代码

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接口

Folowing is the output object interface OutputObject Interface

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 是有效的,我已经检查过了.

I tried to find the reason for this exception but couldn't get it, JSON is valid, I have checked that.

以下是完整的例外情况

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设置)

Newtonsoft.Json.JsonReaderException HResult=0x80131500 Message=Unexpected character encountered while parsing value: {. Path 'outputObject.address', line 17, position 16. Source=Newtonsoft.Json StackTrace: at Newtonsoft.Json.JsonTextReader.ReadStringValue(ReadType readType) at Newtonsoft.Json.JsonTextReader.ReadAsString() at Newtonsoft.Json.JsonReader.ReadForType(JsonContract contract, Boolean hasConverter) at Newtonsoft.Json.Serialization.JsonSerializerInternalReader.PopulateObject(Object newObject, JsonReader reader, JsonObjectContract contract, JsonProperty member, String id) at Newtonsoft.Json.Serialization.JsonSerializerInternalReader.CreateObject(JsonReader reader, Type objectType, JsonContract contract, JsonProperty member, JsonContainerContract containerContract, JsonProperty containerMember, Object existingValue) at Newtonsoft.Json.Serialization.JsonSerializerInternalReader.SetPropertyValue(JsonProperty property, JsonConverter propertyConverter, JsonContainerContract containerContract, JsonProperty containerProperty, JsonReader reader, Object target) at Newtonsoft.Json.Serialization.JsonSerializerInternalReader.PopulateObject(Object newObject, JsonReader reader, JsonObjectContract contract, JsonProperty member, String id) at Newtonsoft.Json.Serialization.JsonSerializerInternalReader.CreateObject(JsonReader reader, Type objectType, JsonContract contract, JsonProperty member, JsonContainerContract containerContract, JsonProperty containerMember, Object existingValue) at Newtonsoft.Json.Serialization.JsonSerializerInternalReader.Deserialize(JsonReader reader, Type objectType, Boolean checkAdditionalContent) at Newtonsoft.Json.JsonSerializer.DeserializeInternal(JsonReader reader, Type objectType) at Newtonsoft.Json.JsonConvert.DeserializeObject(String value, Type type, JsonSerializerSettings settings)

推荐答案

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

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

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

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

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.

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

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

这篇关于解析API响应时遇到意外字符的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆