首页 > 解决方案 > 如何将原始数据列表转换为 Json 列表?

问题描述

我有以下课程:

public class Solution
{

    public string Id { get; set; }
    public string ProjectName { get; set; }
    public string CodeName { get; set; }
    public string Description { get; set; }
    public string Author { get; set; }
    public string Createdate { get; set; }
    public string LastEditedBy { get; set; }
    public string Status { get; set; }
    public string GoLive { get; set; }

    public virtual List<State> States { get; set; }

}


public class State
{
    public Solution Solution { get; set; }
    public string SolutionId { get; set; }
    public string StateId { get; set; }
    public string StateValue { get; set; }
}

其中 StateValue 是原始 json 字符串。

当我想反序列化这个时,我得到一个带有 \r\n 的 json 字符串。

何告诉 json 序列化程序不要转义该字符串并将其视为 json,因为它已经是 json。

我想要这样的输出:

[
{
    "id": "43c7f6d5-61dc-4c1c-8c76-e13878b7483f",
    "projectName": "Test Request 2",
    "codeName": "",
    "description": "",
    "author": "",
    "createdate": "02/13/2019",
    "lastEditedBy": "",
    "status": "Pending",
    "goLive": "02/13/2019",
    "states": [
        {
            "id": "cd7363f8-752b-4eb2-aaa2-ef94d7685153",
            "label": "Empty State",
            "layerone": [
                {
                    "StorageCloudPhysical_Custom3": "cc1",
                    "StorageCloudPhysical_WorkSpace": "ws for asset 2"
                },
                {

                    "StorageCloudPhysical_Custom3": "cc3",
                    "StorageCloudPhysical_WorkSpace": "ws for asset 4"
                }
       } 
     ]
   }
 ]

json 模式中的 states 是操作 Solution.States.Select(s => s.StateValue) 的值,类似于 List。

我怎样才能做到这一点,请提前致谢。

标签: c#json

解决方案


编辑:

你的项目类型是什么?是 C# MVC 吗?

首先,您需要使用 Nuget 安装 NewtonSoft 库,然后在您的控制器上实例化它。

牛顿软件

using Newtonsoft.Json;

现在,您只需使用 serialize 将列表“转换”为 JsonList 即可。

var list = JsonConvert.SerializeObject<List<string>>(YorRawList).ToList();

推荐阅读