JSON.NET DeserializeObject to List of Objects
Asked Answered
H

4

55

I'm trying to Deserialize object to list of object using JSON.NET lib. My json file is:

[
{
    "id": 1,
    "name": "Poczta",
    "description": "Opis",
    "latitude": 52.25197,
    "longitude": 20.896355,
    "accuracy": 0,
    "type": "",
    "image": null
},
{
    "id": 2,
    "name": "WAT",
    "description": "Budynek główny - sztab.\r\nzażółć gęślą jaźń",
    "latitude": 52.2531213,
    "longitude": 20.8995849,
    "accuracy": 0,
    "type": "Uczelnia",
    "image": null
},
{
    "id": 3,
    "name": "Przychodnia",
    "description": "Opis",
    "latitude": 52.250808,
    "longitude": 20.895348,
    "accuracy": 0,
    "type": "",
    "image": null
},
{
    "id": 4,
    "name": "DS3",
    "description": "Opis",
    "latitude": 52.250063,
    "longitude": 20.895847,
    "accuracy": 0,
    "type": "",
    "image": null
},
{
    "id": 5,
    "name": "DS2",
    "description": "Opis",
    "latitude": 52.2497674,
    "longitude": 20.8966583,
    "accuracy": 0,
    "type": "",
    "image": null
},
{
    "id": 6,
    "name": "DS1",
    "description": "Opis",
    "latitude": 52.25088,
    "longitude": 20.897492,
    "accuracy": 0,
    "type": "",
    "image": null
},
{
    "id": 7,
    "name": "DS4",
    "description": "To jest opis",
    "latitude": 52.2539982,
    "longitude": 20.8971716,
    "accuracy": 0,
    "type": "",
    "image": null
},
{
    "id": 15,
    "name": "a",
    "description": "b",
    "latitude": 52.250105,
    "longitude": 20.896124,
    "accuracy": 0,
    "type": "Uczelnia",
    "image": null
}
]

And I wrote some code to do that, but it doesn't work. I tried many options like dynamic deserialize and now i'm tried to make a list.

    async private void webServiceGetPoints()
    {
        try
        {
            var client = new HttpClient();
            var response = await client.GetAsync(new Uri("\\private\\"));
            var result = await response.Content.ReadAsStringAsync();


            List<WebServiceTag> convert = JsonConvert.DeserializeObject<List<WebServiceTag>>(result) as List<WebServiceTag>;

            Debug.WriteLine(convert.Count);
        }
        catch (JsonSerializationException jsonerr)
        {
            Debug.WriteLine(jsonerr.ToString());
        }
        catch (Exception e)
        {
            Debug.WriteLine(e.ToString());
        }
    }

This code based on my own class with is:

class WebServiceTag
{

    [JsonProperty("id")]
    public int id { get; set; }

    [JsonProperty("name")]
    public string name { get; set; }

    [JsonProperty("description")]
    public string description { get; set; }

    [JsonProperty("latitude")]
    public double latitude { get; set; }

    [JsonProperty("longitude")]
    public double longitude { get; set; }

    [JsonProperty("accuracy")]
    public int accuracy { get; set; }

    [JsonProperty("type")]
    public string type { get; set; }

    [JsonProperty("image")]
    public string image { get; set; }        
}
Housing answered 18/1, 2015 at 16:58 Comment(2)
What doesn't work? Do you have a concrete error message?Decree
cannot repro - it works fine for meDominic
D
111

I found that trying to use:

JsonConvert.DeserializeObject<List<T>>()

wouldn't work for me. I found that this worked instead.

JsonConvert.DeserializeObject<IEnumerable<T>>()

Hope its a better late than never answer.

Dermott answered 25/11, 2015 at 9:59 Comment(3)
I wish I could upvote this more than once. I have been Googling for at least an hour for a solution to my problem and this worked nicely. Thank You!Helsinki
Thanks for this. Typo on IEnumerable, but that and a .ToList() on the end got it in the format I needed.Brassbound
Also, check with this article. codeproject.com/Questions/3136037/…Daugava
A
1

If work for u... like it

List<modelCasa> objList = JsonConvert.DeserializeObject<List<modelCasa>>(json);
Ancestry answered 31/3, 2022 at 16:19 Comment(0)
F
0

with using System.Text.Json; (instead of json.net), this works fine:

string data = File.ReadAllText("file.json");   
List<T> MyListOfT = JsonSerializer.Deserialize<List<T>>(data);

This works as long as the top level element of the JSON is an array and not an object.

Also, in your class that you're serializing to, you don't need [JsonProperty("prop")] for your properties or using Newtonsoft.Json; IF you know that the json keys match your properties exactly (case-sensitive). Otherwise, many options are available here: https://learn.microsoft.com/en-us/dotnet/standard/serialization/system-text-json/customize-properties?pivots=dotnet-8-0

Footman answered 7/12, 2023 at 22:14 Comment(0)
P
-2

Response Body Class>>>

    public class RestResponse
    {
        public string status { get; set; }
    }

post Body Class>>>

    class Customer_List
    {
       public string phone { get; set; }
    }

in Void >>>>

        Customer_List send= new Customer_List
        {
            phone= "***"
        };
        //
        string json = JsonConvert.SerializeObject(send);  
        //
        var client = new RestClient("http://localhost:1959/***");
        var request = new RestRequest();
        //
        request.Method = Method.POST;
        request.AddHeader("Accept", "application/json");
        request.Parameters.Clear();
        request.AddParameter("application/json", json, 
        ParameterType.RequestBody);
        //
        var response = client.Execute(request);
        var content = response.Content;

it's answer for me !

      var resp = JsonConvert.DeserializeObject<List<RestResponse>>(content);
Purloin answered 5/12, 2019 at 21:46 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.