首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >从JSON流C#解析url文件时出错

从JSON流C#解析url文件时出错
EN

Stack Overflow用户
提问于 2017-11-17 23:15:14
回答 2查看 1.1K关注 0票数 0

我正在尝试反序列化这个json流:

代码语言:javascript
复制
[{"id":11,"title":"xyz","image":{"url":"/uploads/xxx/yyy/11/pic_1234.jpg"},"target":1}]

这是我用来反序列化流的代码的简化片段:

代码语言:javascript
复制
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()上的布尔类型

EN

回答 2

Stack Overflow用户

发布于 2017-11-17 23:20:34

在JSON片段中,image属性不是字符串,而是一个包含url字符串属性的对象。

因此,您应该具有以下模型:

代码语言:javascript
复制
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; }
}
票数 4
EN

Stack Overflow用户

发布于 2017-11-17 23:21:23

在你的JSon流中,你有一部分

代码语言:javascript
复制
"image": {"url":"/uploads/xxx/yyy/11/pic_1234.jpg"}

这意味着您尝试反序列化的不是字符串,而是一个可以描述为

代码语言:javascript
复制
public class ImagePath {
     [JsonProperty(PropertyName = "url")]
     public string Url { get; set; }
}

在反序列化的类中

代码语言:javascript
复制
[JsonProperty(PropertyName = "image")]    
public ImagePath Image { get; set; }     
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/47353828

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档