Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/289.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/string/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
c#-转换列表<;字符串>;字符串-但在结构上保存_C#_String_List - Fatal编程技术网

c#-转换列表<;字符串>;字符串-但在结构上保存

c#-转换列表<;字符串>;字符串-但在结构上保存,c#,string,list,C#,String,List,我尝试将列表转换为字符串。但我犯了错误,我试着去做,但时间太长了,我需要你的帮助 我想做的是: List<string> list = new List<string> {"a","a","a" }; 我怎么能在一行中做到 谢谢 通过将列表序列化为JSON字符串,您可以很容易地做到这一点。 使用Newtonsoft.Json库 List List=新列表{“a”、“a”、“a”}; var result=JsonConvert.SerializeObject(列表);

我尝试将列表转换为字符串。但我犯了错误,我试着去做,但时间太长了,我需要你的帮助

我想做的是:

List<string> list = new List<string> {"a","a","a" };
我怎么能在一行中做到


谢谢

通过将列表序列化为JSON字符串,您可以很容易地做到这一点。 使用
Newtonsoft.Json

List List=新列表{“a”、“a”、“a”};
var result=JsonConvert.SerializeObject(列表);
使用API(内置.NET Core 3.x,可用于.NET Core 2.x和.NET Framework 4.7.2和4.8)

List List=新列表{“a”、“a”、“a”};
var result=System.Text.Json.JsonSerializer.Serialize(列表);
使用.NET Framework内置类(但不建议使用,首选第一个选项)

List List=新列表{“a”、“a”、“a”};
var result=new JavaScriptSerializer().Serialize(列表);

所有方法都将返回
[“a”,“a”,“a”]
,它们比手动替换更加清晰和方便

通过将列表序列化为JSON字符串,您可以轻松地执行此操作。 使用
Newtonsoft.Json

List List=新列表{“a”、“a”、“a”};
var result=JsonConvert.SerializeObject(列表);
使用API(内置.NET Core 3.x,可用于.NET Core 2.x和.NET Framework 4.7.2和4.8)

List List=新列表{“a”、“a”、“a”};
var result=System.Text.Json.JsonSerializer.Serialize(列表);
使用.NET Framework内置类(但不建议使用,首选第一个选项)

List List=新列表{“a”、“a”、“a”};
var result=new JavaScriptSerializer().Serialize(列表);
所有方法都将返回
[“a”、“a”、“a”]
,它们比手动替换更加清晰和方便

我怎么能在一行中做到

你是否认为如果你打字少一点,你会活得更长

“一行完成”的方法是将问题分解为许多子问题,每个子问题都很短。例如,我们可以通过提取以下子问题来解决您的问题:

引用字符串:

static class Extensions
{
  public static string Quote(this string s) => 
    "\"" + s + "\"";
  public static string Bracket(this string s) =>
    "[" + s + "]";
}
引用一系列字符串:

  public static IEnumerable<string> QuoteAll(
    this IEnumerable<string> items) =>
    items.Select(Quote);
现在把它放在一起:

string result = list.QuoteAll().CommaJoin().Bracket();
“一行”就完成了。但重要的不是每种方法都是一条线。重要的是,首先,通过简单地阅读代码就可以清楚地理解代码的含义,其次,现在您有四种工具可用于程序中的其他工作。这可能不是您要创建的唯一逗号分隔列表

我怎么能在一行中做到

你是否认为如果你打字少一点,你会活得更长

“一行完成”的方法是将问题分解为许多子问题,每个子问题都很短。例如,我们可以通过提取以下子问题来解决您的问题:

引用字符串:

static class Extensions
{
  public static string Quote(this string s) => 
    "\"" + s + "\"";
  public static string Bracket(this string s) =>
    "[" + s + "]";
}
引用一系列字符串:

  public static IEnumerable<string> QuoteAll(
    this IEnumerable<string> items) =>
    items.Select(Quote);
现在把它放在一起:

string result = list.QuoteAll().CommaJoin().Bracket();

“一行”就完成了。但重要的不是每种方法都是一条线。重要的是,首先,通过简单地阅读代码就可以清楚地理解代码的含义,其次,现在您有四种工具可用于程序中的其他工作。这可能不是您要创建的唯一逗号分隔列表。

您的格式几乎是JSON。你熟悉JSON吗?将其序列化为JSON对您来说足够了吗?可能我需要尝试
“[\”“+String.Join(“\”,\”,list)+“\”]您的格式几乎是JSON。你熟悉JSON吗?将其序列化为JSON对您来说足够了吗?可能我需要尝试
“[\”“+String.Join(“\”,\”,list)+“\”]