[英]clean CSV column value as JSON to C# object
I've got some text written in a CSV column that is supposed to represent a JSON string:我在 CSV 列中写了一些文本,该列应该代表 JSON 字符串:
{
"text": "foil text1",
"image": "existing_image_uploaded.png",
"score": false
},
{
"text": "foil text2",
"image": "existing_image_uploaded2.png",
"score": true
}
This CSV text comes out as the following string:此 CSV 文本显示为以下字符串:
var foils = "{\n \"text\": \"foil text1\",\n \"image\": \"existing_image_uploaded.png\",\n
\"score\": false\n},\n\n{\n \"text\": \"foil text2\",\n \"image\":
\"existing_image_uploaded2.png\",\n \"score\": true\n}"
I would like to convert this text to a List of the following class我想将此文本转换为以下 class 的列表
public class FoilJSON{
public string text {get;set;}
public string image {get;set;}
public bool score {get;set;}
}
This is the way I would like to convert the JSON to a List of FoilJSON这是我想将 JSON 转换为 FoilJSON 列表的方式
var converted = JsonSerializer.Deserialize<List<FoilJSON>>(foils);
However the string foils is not in a proper JSON format to convert to a base class.但是,字符串箔不是正确的 JSON 格式,无法转换为基本 class。
Is there a C# library or method to remove all the CSV garbage within the foils string?是否有 C# 库或方法来删除箔字符串中的所有 CSV 垃圾?
var foils = "[{\n \"text\": \"foil text1\",\n \"image\": \"existing_image_uploaded.png\",\n
\"score\": false\n},\n\n{\n \"text\": \"foil text2\",\n \"image\":
\"existing_image_uploaded2.png\",\n \"score\": true\n}]"
You have to have square brackets to have a list otherwise you simply have two objects separated by a comma... doesn't really make it a correct json你必须有方括号才能有一个列表,否则你只是有两个用逗号分隔的对象......并不能真正使它成为正确的 json
try this尝试这个
foils = "[" + foils + "]";
List<FoilJSON> converted = System.Text.Json.JsonSerializer.Deserialize<List<FoilJSON>>(foils);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.