0

I have an comma separated string like this:

string myString = "1,a,b,C1,,#2,d,e,C2,,#3,f,g,C3,,#4,h,i,C4,,#";

This is basically the data from an csv file where I am using reader to read from file.

In the above string ',' represents the data delimeter while '#' represents EOL of the file.

myString = myString.TrimEnd('#'); //Removing extra # in the end.
//Result of above 1,a,b,C1,,#2,d,e,C2,,#3,f,g,C3,,#4,h,i,C4,,

I want to convert the above into multidimentional array, loop through it reading value of each row data and create my own json.

So I started with the below code. This would result me with row and column count.

int rowCount = result.TrimEnd('#').Split('#').Count();
int colCount = result.TrimEnd('#').Split('#')[0].TrimEnd(',').Split(',').Length;

//Defining my object which I want to fill.
JObject myObject = new JObject();

Below I want to loop through row and column getting data value from each row and column

for (int row = o ; row <= rowCount; row++)
{
    for (int col = 0; col <= colCount; col++)
    {
       //So here I want to do something like:
       var rowValue = multiArray[row][col];

       //After getting the row value below is the logic to add to my object
       if(col == 0)
       {
            myObject.Add("first", rowValue);    
       }
       else if(col == colCount)
       {
            myObject.Add("last", rowValue);
       }
       else
       {
            myObject.Add(col, rowValue);
       }
    }
}

So my question is how can I create the multidimentional array "multiArray" in my code.

Example of my json:

{
  "first": 1
  "1": a,
  "2": b,
  "last": C1
},
{
  "first": 2
  "1": c,
  "2": d,
  "last": C2
}
5
  • It would be helpful if you included what you want your final JSON to look like given the starting string "1,a,b,C1,,#2,d,e,C2,,#3,f,g,C3,,#4,h,i,C4,,#" Commented Nov 15, 2018 at 19:12
  • So where are you stuck? Commented Nov 15, 2018 at 19:15
  • @MattBurland my final json would depend upon the rows and columns of the file ie the string. I would take care of creating the json its just that I dont know to create the multi array "multiArray" in my code above from which I can loop through. I have updated my post with json. Commented Nov 15, 2018 at 19:19
  • @OleEHDufour as mentioned in my post, I dont know how to create the "multiarray" so that I can loop through it. Commented Nov 15, 2018 at 19:20
  • @user1563677 - you have an example input, what's the example output for that? Commented Nov 15, 2018 at 21:10

1 Answer 1

1

The following code creates and fills your multi-dimensional array, but there is a problem with your data. Because of the extra commas your json will not look like your sample json.

string myString = "1,a,b,C1,,#2,d,e,C2,,#3,f,g,C3,,#4,h,i,C4,,#".TrimEnd('#');

var rows = myString.Split('#');
var rowCount = rows.Length;
var columnCount = rows[0].Split(',').Length;

string[,] multiArray = new string[rowCount, columnCount];

for (int i = 0; i < rowCount; i ++)
{
    var values = rows[i].Split(',');
    for (int j = 0; j < columnCount && j < values.Length; j++)
    {
        multiArray[i,j] = values[j];
    }
}

The results I get from this are that there is a 4x6 array with only 4 values in each row.

Sign up to request clarification or add additional context in comments.

2 Comments

Thanks will check and get back
I had to adjust a little bit with my code but this works fine.

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.