I am attempting to pull a json but it is not formatted in the traditional way... [List {obj, {obj}, [List {obj}] }]
... I am encountering the following error below, my webClient for deserializing the JSON is not setup correctly... How would I handle the use case where multiple List and Objs are presented in the JSON?
Error
Newtonsoft.Json.JsonSerializationException
HResult=0x80131500
Message=Cannot deserialize the current JSON array (e.g. [1,2,3]) into type 'StatusJSONv1.StatusCollection' because the type requires a JSON object (e.g. {"name":"value"}) to deserialize correctly.
To fix this error either change the JSON to a JSON object (e.g. {"name":"value"}) or change the deserialized type to an array or a type that implements a collection interface (e.g. ICollection, IList) like List<T> that can be deserialized from a JSON array. JsonArrayAttribute can also be added to the type to force it to deserialize from a JSON array.
Path '', line 1, position 1.
Source=Newtonsoft.Json
Classes
namespace StatusJSONv1 {
public class Status
{
string Start { get; set; }
string Desc { get; set; }
UpdateObj update_obj { get; set; }
List<Changes> Changes { get; set; }
string Uri { get; set; }
}
public class UpdateObj
{
string Text { get; set; }
string Final { get; set; }
}
public class Changes
{
string Created { get; set; }
string Text { get; set; }
}
public class StatusCollection
{
private List<Status> statuses;
public List<Status> Statuses { get => statuses; set => statuses = value; }
}
}
using Newtonsoft.JSON I deserialize the json this way
using (var webClient = new WebClient())
{
String rawJSON = webClient.DownloadString("https://somelink.json");
StatusCollection statusCollection = JsonConvert.DeserializeObject<StatusCollection>(rawJSON);
Console.WriteLine(statusCollection.Statuses.Count);
}
my json from the link
[{
start: "2019-03-26T14:30:00Z",
desc: "some description",
update: {
text: "more information",
final: "closing test results"
},
key: "support",
number: 1000,
changes: [{
created: "2019-03-26",
text: "change text 1"
},
{
created: "2019-03-25",
text: "change text 2"
}],
uri: "/some/1001"
}]
User contributions licensed under CC BY-SA 3.0