首页 文章

从JSON流C#解析url fileld时出错[重复]

提问于
浏览
0

这个问题在这里已有答案:

我试图反序列化这个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 HResult = 0x80131500 Message =解析值时遇到意外字符:{ . 路径'[0] .image',第1行,位置171. Source = Newtonsoft.Json StackTrace:Newtonsoft.Json.JsonTextReader.ReadStringValue(ReadType readType)at Newtonsoft.Json.JsonTextReader.ReadAsString()at Newtonsoft.Json.JsonReader Newtonsoft.Json.Serialization.JsonSerializerInternalReader.CopulateObject(对象newObject,JsonReader阅读器,JsonObjectContract Contract ,JsonProperty成员,字符串id)的Newtonoft.Json.Serialization.JsonSerializerInternalReader.CreateObject(JsonReader reader,Type)中的.ReadForType(JsonContract Contract ,布尔hasConverter)在Newtonsoft.Json.Serialization.JsonSerializerInternalReader.CreateValueInternal(JsonReader reader,Type objectType,JsonContract contract,JsonProperty成员,JsonContainerContract containerContract,JsonProperty containerMember,Object existingValue)中的objectType,JsonContract Contract ,JsonProperty成员,JsonContainerContract containerContract,JsonProperty containerMember,Object existingValue) Newtonsoft.Json.Serialization.JsonSeria Newtonoft的Newinoft.Json.Serialization.JsonSerializerInternalReader.CreateList(JsonReader reader,Type objectType,JsonContract contract,JsonProperty member,Object existingValue,String id)的lizerInternalReader.PopulateList(IList列表,JsonReader reader,JsonArrayContract Contract ,JsonProperty containerProperty,String id)在Newtonsoft.Json.Serialization.JsonSerializerInternalReader.Deserialize(JsonReader reader,Type objectType,Boolean checkAdditionalContent)的.Json.Serialization.JsonSerializerInternalReader.CreateValueInternal(JsonReader reader,Type objectType,JsonContract contract,JsonProperty成员,JsonContainerContract containerContract,JsonProperty containerMember,Object existingValue)在Newtonsoft.Json.Json.Json中的Newtonsoft.Json.JsonSerializer.DeserializeInternal(JsonReader reader,Type objectType),在Newtonsoft.Json.JsonConvert.DeserializeObject [T](字符串值,JsonSerializerSettings设置) ngs settings)在New C:\ xxxxx ... \ Program.cs:第19行的Promociones.JsonApi.GetTemplates()中的Newtonsoft.Json.JsonConvert.DeserializeObject [T](字符串值)

2 回答

  • 4

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

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

相关问题