JSON.NET的DeserializeObject方法如何将JSON字符串转换为对象列表

55

我想使用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; }        
}

1
什么不起作用?你有具体的错误信息吗? - Jan Köhler
无法重现 - 对我来说它运行良好。 - Ňɏssa Pøngjǣrdenlarp
3个回答

111

我发现尝试使用:

JsonConvert.DeserializeObject<List<T>>()

对我来说不起作用。我发现这个可以代替。

JsonConvert.DeserializeObject<IEnumerable<T>>()

希望这个回答晚来总比不来好。


3
我希望我能多次点赞。我已经谷歌搜索了至少一个小时,寻找解决我的问题的方法,这个方法很有效。谢谢! - TheAmazingJason
3
谢谢。IEnumerable上有个拼写错误,但加上.ToList()在末尾就可以得到我需要的格式了。 - mjhamre
此外,请查看以下文章。 https://www.codeproject.com/Questions/3136037/Error-with-deserializing-json-data-into-ienumerabl - 米米米

1

如果您从事IT工作并且喜欢它

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

-2

响应体类>>>

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

文章正文类>>>

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

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

这对我来说是答案!

      var resp = JsonConvert.DeserializeObject<List<RestResponse>>(content);

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接