0

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);
3
  • var splited = value.Replace("]", "").Replace("'", "").Replace("[", "").Split(new[] { "," }, StringSplitOptions.RemoveEmptyEntries) Commented Jul 21, 2019 at 11:51
  • @Alex I want to get them in array. for example I want to get like this : ['20190717','7347.00','7291.00','6929.00','7498.00','131','212761','1563195158.00'] and access to each item by index Commented Jul 21, 2019 at 11:54
  • 1
    One option to consider may be to replace the ' with " and then parse it as JSON (as a List<List<string>>). Commented Jul 21, 2019 at 11:54

1 Answer 1

4

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);
    }
}
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.