Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
973 views
in Technique[技术] by (71.8m points)

asp.net mvc - Unexpected character encountered while parsing API response

I am getting this exception:

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

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

CODE

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

MODEL

   public class CarLookupResponse : ICarLookupResponse
    {
        public ICarLookupResult Result { get; set; }

        public ICarLookupOutputObject OutputObject { get; set; }

        public CarLookupResponse()
        {
            Result = new CarLookupResult();
            OutputObject = new CarLookupOutputObject();
        }
    }

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": "[email protected]",
        "address": {
          "apartment": "",
          "state": "CA",
          "city": "BRISBANE",
          "zipCode": "94005",
          "streetAddress": "785, SPITZ BLVD"
        },
        "ssn": "511-04-6666"
      }
    }

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

Following is the full exception deatials

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)

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

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

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...