I have a string in array format and I want convert to an actual array.
'[[1,"MISSING"],[2,"MISSING"],[6,"MISSING"]]'
Is there any way to convert this to array?
I have a string in array format and I want convert to an actual array.
'[[1,"MISSING"],[2,"MISSING"],[6,"MISSING"]]'
Is there any way to convert this to array?
ok first thing... C# strings use double quote but ok ignoring that I am assuming that this is coming from JavaScript and you want to process it in c# is that correct ?
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[][]>();