简体   繁体   English

C# ASP.NET MVC 使用 JSON 将字符串转换为列表

[英]C# ASP.NET MVC convert string to list using JSON

I have an application where I am receiving a datafeed.我有一个应用程序正在接收数据馈送。 The data looks like this after the code below is executed, the variable 'result' looks like this:执行以下代码后数据如下所示,变量“result”如下所示:

[
    { 
        "personName": "Avery Davis",
        "personOrganization": "01"
    },
    {
        "personName": "Chris Davis",
        "personOrganization": "01"
    },
    {
        "personName": "Tony Davis",
        "personOrganization": "01"
    },
    {
        "personName": "Cory Dirt",
        "personOrganization": "01"
    },
    {
        "personName": "Tyler Dirt",
        "personOrganization": "01"
    },
    {
        "personName": "Ann Ford",
        "personOrganization": "01"
    },
    {
        "personName": "Lauren Ford",
        "personOrganization": "01"
    },
    {
        "personName": "Lauren Ford",
        "personOrganization": "01"
    },
    {
        "personName": "Avery Franklin",
        "personOrganization": "01"
    }
]

I have a model class which looks like this:我有一个 model class 看起来像这样:

public class AllPeople
{
    public List<PeopleList> data { get; set; }
}

public class PeopleList
{
    public string personName { get; set; }
    public string personOrganization { get; set; }
}

This is the code that I use to retrieve the data:这是我用来检索数据的代码:

    private IEnumerable<PeopleList> GetPeople()
    {
        IEnumerable<PeopleList> peopleLists = null;

        var client = new WebClient();
        var data = client.DownloadData("https://localhost:44314/api/values");
        var stream = new MemoryStream(data);
        var obj = new DataContractJsonSerializer(typeof(string));
        var result = obj.ReadObject(stream).ToString();

        peopleLists = (IEnumerable<PeopleList>)JsonConvert.DeserializeObject<AllPeople>(result);            

        return peopleLists;
 }

I know the JsonConvert line is incorrect but I have been unsuccessful in figuring out how to convert the string into a list.我知道JsonConvert行不正确,但我一直未能弄清楚如何将字符串转换为列表。

Any suggestions?有什么建议么?

Thanks.谢谢。

In the JSON shown there's no property called data .在显示的 JSON 中,没有名为data的属性。 So this isn't an instance of AllPeople .所以这不是AllPeople的一个实例。 The JSON shown is just an array of PeopleList .显示的 JSON 只是PeopleList的数组。 (Which, incidentally, is a terrible name for a single instance of a "person" .) (顺便说一下,对于“人”的单个实例来说,这是一个可怕的名字。)

Deserialize it to a List<PeopleList> :将其反序列List<PeopleList>

peopleLists = JsonConvert.DeserializeObject<List<PeopleList>>(result);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 C# - ASP.NET MVC - 将对象列表转换为字符串列表,以在会话中使用 - C# - ASP.NET MVC - Convert a List of object , to a List of string, to use in a Session 如何使用JSON.NET(C#/ ASP.NET)遍历列表并将其转换为JSON对象 - How to iterate through a List and convert it to a JSON object using JSON.NET (C# / ASP.NET) 在ASP.NET C#中将数组转换为JSON字符串 - Convert Array into JSON String in ASP.NET C# 如何使用C#在ASP.Net中将Json字符串转换为DataTable - How to Convert Json String into DataTable in ASP.Net with C# 使用C#ASP.NET MVC异步将JSON字符串发布到RESTful API - Post JSON string to RESTful API using C# ASP.NET MVC async 清单 <string> 用Json asp.net mvc - List<string> with Json asp.net mvc 使用ASP.NET MVC 2和C#打印文档或字符串 - Printning document or string using ASP.NET MVC 2 and C# 如何在 asp.net 核心 mvc ZD7EFA19FBE7D39372FD5ADB60242 中将列表 object 转换为 Viewmodel object? - How can I convert List object to Viewmodel object in asp.net core mvc C#? 一个简单的问题:将字符串转换为c#asp.net mvc中的图像标签 - Simple Question: convert a string to an image tag in c# asp.net mvc 为什么我不能从 int 转换为字符串 C# ASP.NET CORE MVC - Why I cannot convert from int to string C# ASP.NET CORE MVC
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM