C# 将数组格式的字符串转换为C中的数组#

C# 将数组格式的字符串转换为C中的数组#,c#,C#,我有一个数组格式的字符串,我想转换为实际数组 '[[1,"MISSING"],[2,"MISSING"],[6,"MISSING"]]' 有没有办法将其转换为数组 好的,首先。。。C#字符串使用双引号,但我假设这是来自JavaScript的,您希望用C#处理它,对吗 var source= "[[1,\"MISSING\"],[2,\"MISSING\"],[6,\"MISSING\"]]"; // doing it by hand. you could clearly do this mor

我有一个数组格式的字符串,我想转换为实际数组

'[[1,"MISSING"],[2,"MISSING"],[6,"MISSING"]]'
有没有办法将其转换为数组

好的,首先。。。C#字符串使用双引号,但我假设这是来自JavaScript的,您希望用C#处理它,对吗

var source= "[[1,\"MISSING\"],[2,\"MISSING\"],[6,\"MISSING\"]]";
// doing it by hand. you could clearly do this more consisely but verbose like this makes it easy to follow i think 
var arrayWithNoExternalCode = new string[10];
var arrayItems = source.Replace("[[","").Replace("]]","").Split(new[] { "],["},StringSplitOptions.RemoveEmptyEntries);
foreach (var item in arrayItems)
{
var parts = item.Split(new[] { ",\""},StringSplitOptions.RemoveEmptyEntries);
var index = parts[0];
var indexValue = parts[1].Replace("\"", "");
Console.WriteLine($"array index: {index}='{indexValue}'");
arrayWithNoExternalCode[Convert.ToInt32(index)] = indexValue;
}
// add using ServiceStack.Text
// via nuget: Install-Package ServiceStack.Text -Version 5.5.0
var easyArray = source.FromJson<string[][]>();
var source=“[1,\'MISSING\'],[2,\'MISSING\'],[6,\'MISSING\']”;
//用手做。很明显,你可以更仔细地做这件事,但是像这样冗长的话很容易理解
var arrayWithNoExternalCode=新字符串[10];
var arrayItems=source.Replace(“[[”,”).Replace(“]]”,“).Split(新的[]{”,[“},StringSplitOptions.RemoveEmptyEntries);
foreach(arrayItems中的var项)
{
var parts=item.Split(新的[]{,\”“},StringSplitOptions.RemoveEmptyEntries);
var指数=部分[0];
var indexValue=parts[1]。替换(“\”,”);
WriteLine($“数组索引:{index}='{indexValue}'”;
无外部代码[Convert.ToInt32(索引)]=索引值的数组;
}
//使用ServiceStack.Text添加
//通过nuget:安装ServiceStack.Text包-版本5.5.0
var easyArray=source.FromJson();

< /代码>使用一个不有效的JSON.@ ITSMES8,只有当你认为单引号是完整字符串的一部分时,你能显示实际的字符串声明吗?这样就不会对字符串中的内容有什么混淆了?也许类似于:“代码>字符串值=”[(1,\“缺少”],[ 2,“丢失”],[ 6,“丢失] ] ]
这是我从已经开发了很多代的遗留数据库表字段中检索到的实际字符串,没有文档说明它们为什么以这种方式保存?@RufusL根据问题的性质给出了一个很容易理解/理解的回答。