JSON.NET DeserializeObject到对象列表 [英] JSON.NET DeserializeObject to List of Objects

查看:568
本文介绍了JSON.NET DeserializeObject到对象列表的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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

推荐答案

我发现尝试使用:

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.

这篇关于JSON.NET DeserializeObject到对象列表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆