JSON.NET DeserializeObject到对象列表

Cri*_*iss 32 c# json.net windows-store-apps windows-phone-8.1

我正在尝试使用JSON.NET lib将对象反序列化为对象列表.我的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
}
]
Run Code Online (Sandbox Code Playgroud)

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

    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());
        }
    }
Run Code Online (Sandbox Code Playgroud)

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

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; }        
}
Run Code Online (Sandbox Code Playgroud)

那是我这个问题的第二天,所以请帮帮我:)应该这么容易.

chr*_*389 62

我发现尝试使用:

JsonConvert.DeserializeObject<List<T>>()
Run Code Online (Sandbox Code Playgroud)

对我不起作用.我发现这有效.

JsonConvert.DeserializeObject<IEnumerable<T>>()
Run Code Online (Sandbox Code Playgroud)

希望它迟到总比没有回答好.

  • 我希望我能不止一次投票。我已经用谷歌搜索了至少一个小时来解决我的问题,这很好用。谢谢你! (3认同)
  • 谢谢你.在IEnumerable上的错字,但是那个和.ToList()最终得到了我需要的格式. (2认同)