JSON.NET 将对象反序列化为对象列表

问题描述 投票:0回答:4

我正在尝试使用 JSON.NET 库将对象反序列化为对象列表。我的 json 文件是:

[
{
    "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
}
]

我写了一些代码来做到这一点,但它不起作用。我尝试了很多选项,例如动态反序列化,现在我尝试列出一个列表。

    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());
        }
    }

这个基于我自己的类的代码是:

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; }        
}
c# windows-store-apps json.net windows-phone-8.1
4个回答
111
投票

我发现尝试使用:

JsonConvert.DeserializeObject<List<T>>()

对我不起作用。我发现这反而有效。

JsonConvert.DeserializeObject<IEnumerable<T>>()

希望迟到总比不回答好。


1
投票

如果对你有用...喜欢它

List<modelCasa> objList = JsonConvert.DeserializeObject<List<modelCasa>>(json);

0
投票

使用

using System.Text.Json;
(而不是 json.net),效果很好:

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

只要 JSON 的顶级元素是数组而不是对象,此方法就有效。

此外,在您要序列化到的类中,您的属性不需要

[JsonProperty("prop")]
,或者如果您知道 json 键与您的属性完全匹配(区分大小写),则不需要
using Newtonsoft.Json;
。否则,这里有许多选项可用: https://learn.microsoft.com/en-us/dotnet/standard/serialization/system-text-json/customize-properties?pivots=dotnet-8-0


-2
投票

响应主体类>>>

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

身体课后>>>

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

虚空>>>>

        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;

这是我的答案!

      var resp = JsonConvert.DeserializeObject<List<RestResponse>>(content);
© www.soinside.com 2019 - 2024. All rights reserved.