Removing unserialized unnamed array

I find it difficult to write the appropriate annotations to represent the data that is returned from the JSON Get request, which returns the data as follows:

[{"ProductCode":"0129923083091","Description":"DIESEL ","SalesLitres":6058.7347,"SalesValue":6416.2000},{"ProductCode":"0134039344902","Description":"UNLEADED ","SalesLitres":3489.8111,"SalesValue":3695.7100}, ... ] 

(the ellipsis above just indicating that I could have a variable number of these elements)

In my model class (I use the MVVM approach for the Xamarin project, but it’s not so important) I use annotations to represent the attributes of the model

  namespace App8.Models { public class ReportRow { [JsonProperty("ProductCode")] public string ProductCode { get; set; } = string.Empty; [JsonProperty("Description")] public string Description { get; set; } = string.Empty; [JsonProperty("SalesLitres")] public double SalesLitres { get; set; } = 0.0; [JsonProperty("SalesValue")] public double SalesValue { get; set; } = 0.0; } } 

I would like to point out another class that shows the container / contained ratio. However, I'm looking up because the annotation does not have a JSON attribute to represent the "root" of the returned collection.

I would not have a problem displaying JSON in the object model for any JSON arrays that are called in the returned JSON. In this case, I could create another class with the specified JSON attribute that contained the C # list, but I am trying to provide an appropriate model mapping for JSON that returns a list of elements in an unnamed array.

Any ideas how I could approach this?

+5
source share
1 answer

To deserialize this JSON, use:

 JsonConvert.DeserializeObject<List<ReportRow>>(json) 

(or whatever option you want, the key here asks to deserialize the ReportRow . It could be your own class that implements ICollection or any of the built-in functions)

The same goes for JsonTextReader or any other JSON.NET deserialization tool. Just use ICollection<YourType> as the target type.

+4
source

Source: https://habr.com/ru/post/1257610/


All Articles