c# - 如何将原始数据列表转换为 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# MVC 吗?
首先,您需要使用 Nuget 安装 NewtonSoft 库,然后在您的控制器上实例化它。
using Newtonsoft.Json;
现在,您只需使用 serialize 将列表“转换”为 JsonList 即可。
var list = JsonConvert.SerializeObject<List<string>>(YorRawList).ToList();
推荐阅读
- java - 配置 Spring MVC 和 Hibernate 时 sessionFactory 出错
- sql - 如何将连接转换为嵌套选择?
- java - 有什么方法可以在不创建循环的情况下运行此代码
- .net - 1 班轮 RegEx 查找下一个可用号码
- php - OJS 3.1.1.4 插件选项卡未加载
- c# - C# Unity 字典使用密钥打开 .csv 文件
- javascript - 仅使用 js 时如何打印选定的下拉列表选项和文本字段?
- android - 如何在自定义形状的矢量可绘制对象周围生成阴影?
- nginx - Nginx 重定向除索引以外的所有内容
- python - ImportError: DLL load failed, while file is in working directory