【问题标题】:Error parsing url fileld from JSON stream C# [duplicate]从 JSON 流 C# 解析 url 字段时出错 [重复]
【发布时间】:2017-11-17 15:15:14
【问题描述】:

我正在尝试反序列化这个 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解析图像字段抛出异常:

...解析值时遇到意外字符:{。路径'[0].image',...

这是完全的例外:

Newtonsoft.Json.JsonReaderException 发生 H结果=0x80131500 消息=解析值时遇到意外字符:{。路径“[0].image”,第 1 行,位置 171。 来源=Newtonsoft.Json 堆栈跟踪: 在 Newtonsoft.Json.JsonTextReader.ReadStringValue(ReadType readType) 在 Newtonsoft.Json.JsonTextReader.ReadAsString() 在 Newtonsoft.Json.JsonReader.ReadForType(JsonContract 合同,布尔 hasConverter) 在 Newtonsoft.Json.Serialization.JsonSerializerInternalReader.PopulateObject(对象 newObject,JsonReader 阅读器,JsonObjectContract 合同,JsonProperty 成员,字符串 id) 在 Newtonsoft.Json.Serialization.JsonSerializerInternalReader.CreateObject(JsonReader 阅读器,类型 objectType,JsonContract 合同,JsonProperty 成员,JsonContainerContract containerContract,JsonProperty containerMember,对象现有值) 在 Newtonsoft.Json.Serialization.JsonSerializerInternalReader.CreateValueInternal(JsonReader 阅读器,类型 objectType,JsonContract 合同,JsonProperty 成员,JsonContainerContract containerContract,JsonProperty containerMember,对象现有值) 在 Newtonsoft.Json.Serialization.JsonSerializerInternalReader.PopulateList(IList 列表,JsonReader 阅读器,JsonArrayContract 合同,JsonProperty containerProperty,字符串 id) 在 Newtonsoft.Json.Serialization.JsonSerializerInternalReader.CreateList(JsonReader 阅读器,类型 objectType,JsonContract 合同,JsonProperty 成员,对象现有值,字符串 id) 在 Newtonsoft.Json.Serialization.JsonSerializerInternalReader.CreateValueInternal(JsonReader 阅读器,类型 objectType,JsonContract 合同,JsonProperty 成员,JsonContainerContract containerContract,JsonProperty containerMember,对象现有值) 在 Newtonsoft.Json.Serialization.JsonSerializerInternalReader.Deserialize(JsonReader 阅读器,类型 objectType,布尔 checkAdditionalContent) 在 Newtonsoft.Json.JsonSerializer.DeserializeInternal(JsonReader 阅读器,类型 objectType) 在 Newtonsoft.Json.JsonConvert.DeserializeObject(字符串值,类型类型,JsonSerializerSettings 设置) 在 Newtonsoft.Json.JsonConvert.DeserializeObject[T](字符串值,JsonSerializerSettings 设置) 在 Newtonsoft.Json.JsonConvert.DeserializeObject[T](字符串值) 在 C:\xxxxx...\Program.cs:line 19 中的 Promociones.JsonApi.GetTemplates() 处

【问题讨论】:

  • 请发布完整错误,包括堆栈。
  • ...还有json的内容
  • url 当json尝试解析它时它只是字符串...不是图像..所以很明显它会抛出那个错误。
  • 你有 Image 作为 string 但在 json 中它是一个对象。
  • TemplateImage 只是一个字符串,但在json 中它是具有url 属性的对象...

标签: c# json windows serialization


【解决方案1】:

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

【讨论】:

    【解决方案2】:

    在你的 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; }     
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2021-11-22
      • 1970-01-01
      • 1970-01-01
      • 2013-08-23
      • 2019-06-30
      • 1970-01-01
      • 1970-01-01
      • 2020-02-08
      相关资源
      最近更新 更多