programmer
programmer

Reputation: 83

How to make a string with array list iterable

I have a value like below

[['20190717','7347.00','7291.00','6929.00','7498.00','131','212761','1563195158.00'],['20190716','7291.00','7584.00','7205.00','7800.00','205','786281','5732513136.00'],['20190715','7584.00','7561.00','7262.00','7911.00','201','957135','7258611941.00'],['20190714','7561.00','7796.00','7407.00','7909.00','411','1715426','12970206490.00'],['20190713','7796.00','7428.00','7602.00','7799.00','468','2622089','20442582710.00'],['20190710','7428.00','7140.00','7070.00','7497.00','250','947330','7037099454.00'],['20190709','7140.00','7442.00','7070.00','7449.00','219','665235','4749481884.00'],['20190708','7442.00','7164.00','7300.00','7522.00','276','1171931','8721432059.00'],['20190707','7164.00','6882.00','6540.00','7226.00','324','1125954','8066782953.00']]

So I want to use above data in a foreach statement and access each value of the array item like item[0]. I tried like this but it is not what I wanted and I can not get value by index

var splited = value.Replace("[[", "[").Replace("]]", "]").Split(new string[] { "],"}, StringSplitOptions.RemoveEmptyEntries);

Upvotes: 0

Views: 58

Answers (1)

Jesse de Wit
Jesse de Wit

Reputation: 4177

It is recommended to 'deserialize' this, before using it. Since your example looks like json, you could add a reference to the nuget package NewtonSoft.Json and deserialize like so:

string json = "[['20190717','7347.00','7291.00','6929.00','7498.00','131','212761','1563195158.00'],['20190716','7291.00','7584.00','7205.00','7800.00','205','786281','5732513136.00'],['20190715','7584.00','7561.00','7262.00','7911.00','201','957135','7258611941.00'],['20190714','7561.00','7796.00','7407.00','7909.00','411','1715426','12970206490.00'],['20190713','7796.00','7428.00','7602.00','7799.00','468','2622089','20442582710.00'],['20190710','7428.00','7140.00','7070.00','7497.00','250','947330','7037099454.00'],['20190709','7140.00','7442.00','7070.00','7449.00','219','665235','4749481884.00'],['20190708','7442.00','7164.00','7300.00','7522.00','276','1171931','8721432059.00'],['20190707','7164.00','6882.00','6540.00','7226.00','324','1125954','8066782953.00']]";
List<List<string>> deserializedList = JsonConvert.DeserializeObject<List<List<string>>>(json);
foreach (List<string> innerList in deserializedList)
{
    foreach (string item in innerList)
    {
        Console.WriteLine(item);
    }
}

Upvotes: 4

Related Questions