我正在尝试反序列化这个json流:
[{"id":11,"title":"xyz","image":{"url":"/uploads/xxx/yyy/11/pic_1234.jpg"},"target":1}]这是我用来反序列化流的代码的简化片段:
public class Template
{
[JsonProperty(PropertyName = "id")]
public string Id { get; set; }
[JsonProperty(PropertyName = "title")]
public string Title { get; set; }
[JsonProperty(PropertyName = "image")]
public string Image { get; set; }
[JsonProperty(PropertyName = "target")]
public string Target { get; set; }
}
string url = @"http://my-url-here";
IList<Template> templates = new List<Template>();
using (var webClient = new WebClient())
{
var json = webClient.DownloadString(url);
templates = JsonConvert.DeserializeObject<List<Template>>(json);
...
}JsonConvert.DeserializeObject在解析图像字段时抛出异常:
分析值{时遇到...Unexpected字符。路径'.image',...
这是完整的例外:
分析值{时出现Newtonsoft.Json.JsonReaderException HResult=0x80131500 Message=Unexpected字符。路径'.image',第1行,位置171。Boolean StackTrace: at Newtonsoft.Json.JsonTextReader.ReadStringValue(ReadType readType) at Newtonsoft.Json.JsonTextReader.ReadAsString() at Newtonsoft.Json.Serialization.JsonSerializerInternalReader.PopulateObject(Object contract,Boolean StackTrace) at Newtonsoft.Json.Serialization.JsonSerializerInternalReader.CreateValueInternal(JsonReader newObject,JsonReader reader,JsonObjectContract contract,JsonProperty成员,String id) at Newtonsoft.Json.Serialization.JsonSerializerInternalReader.CreateObject(JsonReader reader,类型objectType,JsonContract contract,JsonProperty成员,JsonContainerContract containerContract,JsonProperty,Object ) at reader,类型,位于Newtonsoft.Json.Serialization.JsonSerializerInternalReader.PopulateList(IList读取器的JsonContract协定,JsonProperty成员,JsonContainerContract containerContract,JsonProperty containerMember,对象existingValue),位于Newtonsoft.Json.Serialization.JsonSerializerInternalReader.CreateList(JsonReader读取器的类型objectType,JsonArrayContract协定,JsonProperty containerProperty,字符串id),位于containerContract读取器的类型objectType,协定,成员,,,对象)位于读取器,类型,Newtonsoft.Json.JsonSerializer.DeserializeInternal(JsonReader读卡器上的布尔值,Newtonsoft.Json.JsonConvert.DeserializeObject(String值上的objectType),类型类型,JsonSerializerSettings设置) C:\xxxxx...\Program.cs:line 19中Newtonsoft.Json.JsonConvert.DeserializeObjectT上的Promociones.JsonApi.GetTemplates()上的布尔类型
发布于 2017-11-17 23:20:34
在JSON片段中,image属性不是字符串,而是一个包含url字符串属性的对象。
因此,您应该具有以下模型:
public class Image
{
[JsonProperty(PropertyName = "url")]
public string Url { get; set; }
}
public class Template
{
[JsonProperty(PropertyName = "id")]
public string Id { get; set; }
[JsonProperty(PropertyName = "title")]
public string Title { get; set; }
[JsonProperty(PropertyName = "image")]
public Image Image { get; set; }
[JsonProperty(PropertyName = "target")]
public string Target { get; set; }
}发布于 2017-11-17 23:21:23
在你的JSon流中,你有一部分
"image": {"url":"/uploads/xxx/yyy/11/pic_1234.jpg"}这意味着您尝试反序列化的不是字符串,而是一个可以描述为
public class ImagePath {
[JsonProperty(PropertyName = "url")]
public string Url { get; set; }
}在反序列化的类中
[JsonProperty(PropertyName = "image")]
public ImagePath Image { get; set; } https://stackoverflow.com/questions/47353828
复制相似问题