Pra*_*n.G 2 asp.net-mvc json.net asp.net-web-api
我收到此异常:
Newtonsoft.Json.JsonReaderException HResult = 0x80131500 Message =解析值{时遇到意外字符。路径“ outputObject.address”,第17行,位置16。
从API反序列化响应数据时。(帖子末尾完全例外)
码
return JsonConvert.DeserializeObject(webResponseEntity.ResponseData, typeof(CarLookupResponse)) as CarLookupResponse;
Run Code Online (Sandbox Code Playgroud)
模型
public class CarLookupResponse : ICarLookupResponse
{
public ICarLookupResult Result { get; set; }
public ICarLookupOutputObject OutputObject { get; set; }
public CarLookupResponse()
{
Result = new CarLookupResult();
OutputObject = new CarLookupOutputObject();
}
}
Run Code Online (Sandbox Code Playgroud)
以下是输出对象接口 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; }
}
Run Code Online (Sandbox Code Playgroud)
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"
}
}
Run Code Online (Sandbox Code Playgroud)
我试图找到此异常的原因,但无法获取,JSON有效,我已经检查过了。
以下是全部例外情况
Newtonsoft.Json.JsonReaderException HResult = 0x80131500 Message =解析值{时遇到意外字符。路径'outputObject.address',第17行,位置16。Source = Newtonsoft.Json StackTrace:位于Newtonsoft.Json.JsonTextReader.ReadAsString()的Newtonsoft.Json.JsonTextReader.ReadStringValue(ReadType readType),位于Newtonsoft.Json.JsonReader.ReadForType (Newsonsoft.Json.Serialization.JsonSerializerInternalReader.PopulateObject(Object newObject,JsonReader reader,JsonObjectContract contract,JsonProperty member,String ID)的Newtonsoft.Json.Serialization.JsonSerializerInternalReader。 JsonContract合同,JsonProperty成员,
您的问题是您已声明CarLookupOutputObject.Address
为string
,但相应的JSON值是一个对象:
"address": {
"apartment": "",
...
},
Run Code Online (Sandbox Code Playgroud)
如其序列化指南中所述,仅原始.Net类型和可转换为的类型string
被序列化为JSON字符串。由于的值"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; }
}
Run Code Online (Sandbox Code Playgroud)
归档时间: |
|
查看次数: |
9418 次 |
最近记录: |