Error parsing url fileld from JSON stream C#

0

I am trying to deserialze this json stream:

[{"id":11,"title":"xyz","image":{"url":"/uploads/xxx/yyy/11/pic_1234.jpg"},"target":1}]

This is the simplified fragment of the code I am using to deserialze the stream:

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 throws an exception parsing image field:

...Unexpected character encountered while parsing value: {. Path '[0].image',...

This is the full exception:

Newtonsoft.Json.JsonReaderException occurred HResult=0x80131500 Message=Unexpected character encountered while parsing value: {. Path '[0].image', line 1, position 171. Source=Newtonsoft.Json StackTrace: at Newtonsoft.Json.JsonTextReader.ReadStringValue(ReadType readType) at Newtonsoft.Json.JsonTextReader.ReadAsString() at Newtonsoft.Json.JsonReader.ReadForType(JsonContract contract, Boolean hasConverter) at Newtonsoft.Json.Serialization.JsonSerializerInternalReader.PopulateObject(Object newObject, JsonReader reader, JsonObjectContract contract, JsonProperty member, String id) at Newtonsoft.Json.Serialization.JsonSerializerInternalReader.CreateObject(JsonReader reader, Type objectType, JsonContract contract, JsonProperty member, JsonContainerContract containerContract, JsonProperty containerMember, Object existingValue) at Newtonsoft.Json.Serialization.JsonSerializerInternalReader.CreateValueInternal(JsonReader reader, Type objectType, JsonContract contract, JsonProperty member, JsonContainerContract containerContract, JsonProperty containerMember, Object existingValue) at Newtonsoft.Json.Serialization.JsonSerializerInternalReader.PopulateList(IList list, JsonReader reader, JsonArrayContract contract, JsonProperty containerProperty, String id) at Newtonsoft.Json.Serialization.JsonSerializerInternalReader.CreateList(JsonReader reader, Type objectType, JsonContract contract, JsonProperty member, Object existingValue, String id) at Newtonsoft.Json.Serialization.JsonSerializerInternalReader.CreateValueInternal(JsonReader reader, Type objectType, JsonContract contract, JsonProperty member, JsonContainerContract containerContract, JsonProperty containerMember, Object existingValue) at Newtonsoft.Json.Serialization.JsonSerializerInternalReader.Deserialize(JsonReader reader, Type objectType, Boolean checkAdditionalContent) at Newtonsoft.Json.JsonSerializer.DeserializeInternal(JsonReader reader, Type objectType) at Newtonsoft.Json.JsonConvert.DeserializeObject(String value, Type type, JsonSerializerSettings settings) at Newtonsoft.Json.JsonConvert.DeserializeObject[T](String value, JsonSerializerSettings settings) at Newtonsoft.Json.JsonConvert.DeserializeObject[T](String value) at Promociones.JsonApi.GetTemplates() in C:\xxxxx...\Program.cs:line 19

c#
json
windows
serialization
asked on Stack Overflow Nov 17, 2017 by Raul Lopez • edited Nov 17, 2017 by Raul Lopez

2 Answers

4

In the JSON fragment, the image property is not a string, but an object that contains the url string property.

Therefore, you should have the following model:

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; }
}
answered on Stack Overflow Nov 17, 2017 by Cristian Lupascu
0

In Your JSon Stream You have part

"image": {"url":"/uploads/xxx/yyy/11/pic_1234.jpg"}

and it means that You try to deserialize not string but an object which could be described as

public class ImagePath {
     [JsonProperty(PropertyName = "url")]
     public string Url { get; set; }
}

and In Your deserialized class

[JsonProperty(PropertyName = "image")]    
public ImagePath Image { get; set; }     
answered on Stack Overflow Nov 17, 2017 by paqao

User contributions licensed under CC BY-SA 3.0