Creating a multidimensional array from a string with delimiter










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










share|improve this question
























  • 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,,#"

    – Matt Burland
    Nov 15 '18 at 19:12











  • So where are you stuck?

    – Ole EH Dufour
    Nov 15 '18 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.

    – user1563677
    Nov 15 '18 at 19:19












  • @OleEHDufour as mentioned in my post, I dont know how to create the "multiarray" so that I can loop through it.

    – user1563677
    Nov 15 '18 at 19:20











  • @user1563677 - you have an example input, what's the example output for that?

    – Matt Burland
    Nov 15 '18 at 21:10















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










share|improve this question
























  • 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,,#"

    – Matt Burland
    Nov 15 '18 at 19:12











  • So where are you stuck?

    – Ole EH Dufour
    Nov 15 '18 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.

    – user1563677
    Nov 15 '18 at 19:19












  • @OleEHDufour as mentioned in my post, I dont know how to create the "multiarray" so that I can loop through it.

    – user1563677
    Nov 15 '18 at 19:20











  • @user1563677 - you have an example input, what's the example output for that?

    – Matt Burland
    Nov 15 '18 at 21:10













0












0








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










share|improve this question
















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







c#






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 15 '18 at 19:23







user1563677

















asked Nov 15 '18 at 19:09









user1563677user1563677

153217




153217












  • 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,,#"

    – Matt Burland
    Nov 15 '18 at 19:12











  • So where are you stuck?

    – Ole EH Dufour
    Nov 15 '18 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.

    – user1563677
    Nov 15 '18 at 19:19












  • @OleEHDufour as mentioned in my post, I dont know how to create the "multiarray" so that I can loop through it.

    – user1563677
    Nov 15 '18 at 19:20











  • @user1563677 - you have an example input, what's the example output for that?

    – Matt Burland
    Nov 15 '18 at 21:10

















  • 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,,#"

    – Matt Burland
    Nov 15 '18 at 19:12











  • So where are you stuck?

    – Ole EH Dufour
    Nov 15 '18 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.

    – user1563677
    Nov 15 '18 at 19:19












  • @OleEHDufour as mentioned in my post, I dont know how to create the "multiarray" so that I can loop through it.

    – user1563677
    Nov 15 '18 at 19:20











  • @user1563677 - you have an example input, what's the example output for that?

    – Matt Burland
    Nov 15 '18 at 21:10
















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,,#"

– Matt Burland
Nov 15 '18 at 19:12





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,,#"

– Matt Burland
Nov 15 '18 at 19:12













So where are you stuck?

– Ole EH Dufour
Nov 15 '18 at 19:15





So where are you stuck?

– Ole EH Dufour
Nov 15 '18 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.

– user1563677
Nov 15 '18 at 19:19






@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.

– user1563677
Nov 15 '18 at 19:19














@OleEHDufour as mentioned in my post, I dont know how to create the "multiarray" so that I can loop through it.

– user1563677
Nov 15 '18 at 19:20





@OleEHDufour as mentioned in my post, I dont know how to create the "multiarray" so that I can loop through it.

– user1563677
Nov 15 '18 at 19:20













@user1563677 - you have an example input, what's the example output for that?

– Matt Burland
Nov 15 '18 at 21:10





@user1563677 - you have an example input, what's the example output for that?

– Matt Burland
Nov 15 '18 at 21:10












2 Answers
2






active

oldest

votes


















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.






share|improve this answer























  • Thanks will check and get back

    – user1563677
    Nov 15 '18 at 20:11











  • I had to adjust a little bit with my code but this works fine.

    – user1563677
    Nov 16 '18 at 15:08


















0














The below code does the trick for me and spits out the following json:



[

"first": 1,
"one": "a",
"two": "b",
"last": "C1"
,

"first": 2,
"one": "d",
"two": "e",
"last": "C2"
,

"first": 3,
"one": "f",
"two": "g",
"last": "C3"
,

"first": 4,
"one": "h",
"two": "i",
"last": "C4"

]


You will need to install Newtonsoft.Json via your package manager.



using System;
using System.Collections.Generic;
using System.Linq;
using System.Text.RegularExpressions;
using Newtonsoft.Json;

namespace ConsoleApp2

class Program

class WahEvah

public int first;
public string one;
public string two;
public string last;



static void Main(string args)

List<string> myString = "1,a,b,C1,,#2,d,e,C2,,#3,f,g,C3,,#4,h,i,C4,,#".Split('#').ToList();
List<List<string>> myString2 = myString.Select(x => x.Split(',').ToList()).ToList();
List<WahEvah> l = new List<WahEvah>();
int counter = 0;
for (int i = 0; i < myString2.Count - 1; i++)

WahEvah wd = NewMethod(myString2[i], counter);
l.Add(wd);


string json = JsonConvert.SerializeObject(l, Formatting.Indented);
Console.Write(json);
Console.ReadLine();


private static WahEvah NewMethod(List<string> myString, int counter)

counter = 0;
WahEvah w = null; w = new WahEvah();
foreach (string s2 in myString)



if (counter == 0)

w.first = Convert.ToInt32(s2.Trim()); counter++; continue;

if (counter == 1)

w.one = s2.Trim(); counter++; continue;

if (counter == 2)

w.two = s2.Trim(); counter++; continue;

if (counter == 3)

w.last = s2.Trim(); counter++; continue;





return w;










share|improve this answer























  • Thanks for the inputs but I dont have a static list so cant create a fixed model as you have created.

    – user1563677
    Nov 16 '18 at 15:09











  • It's just an example. It should work with any string that follows your pattern.

    – Ole EH Dufour
    Nov 16 '18 at 15:42










Your Answer






StackExchange.ifUsing("editor", function ()
StackExchange.using("externalEditor", function ()
StackExchange.using("snippets", function ()
StackExchange.snippets.init();
);
);
, "code-snippets");

StackExchange.ready(function()
var channelOptions =
tags: "".split(" "),
id: "1"
;
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function()
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled)
StackExchange.using("snippets", function()
createEditor();
);

else
createEditor();

);

function createEditor()
StackExchange.prepareEditor(
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader:
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
,
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
);



);













draft saved

draft discarded


















StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53326403%2fcreating-a-multidimensional-array-from-a-string-with-delimiter%23new-answer', 'question_page');

);

Post as a guest















Required, but never shown

























2 Answers
2






active

oldest

votes








2 Answers
2






active

oldest

votes









active

oldest

votes






active

oldest

votes









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.






share|improve this answer























  • Thanks will check and get back

    – user1563677
    Nov 15 '18 at 20:11











  • I had to adjust a little bit with my code but this works fine.

    – user1563677
    Nov 16 '18 at 15:08















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.






share|improve this answer























  • Thanks will check and get back

    – user1563677
    Nov 15 '18 at 20:11











  • I had to adjust a little bit with my code but this works fine.

    – user1563677
    Nov 16 '18 at 15:08













1












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.






share|improve this answer













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.







share|improve this answer












share|improve this answer



share|improve this answer










answered Nov 15 '18 at 19:49









JamesJames

936




936












  • Thanks will check and get back

    – user1563677
    Nov 15 '18 at 20:11











  • I had to adjust a little bit with my code but this works fine.

    – user1563677
    Nov 16 '18 at 15:08

















  • Thanks will check and get back

    – user1563677
    Nov 15 '18 at 20:11











  • I had to adjust a little bit with my code but this works fine.

    – user1563677
    Nov 16 '18 at 15:08
















Thanks will check and get back

– user1563677
Nov 15 '18 at 20:11





Thanks will check and get back

– user1563677
Nov 15 '18 at 20:11













I had to adjust a little bit with my code but this works fine.

– user1563677
Nov 16 '18 at 15:08





I had to adjust a little bit with my code but this works fine.

– user1563677
Nov 16 '18 at 15:08













0














The below code does the trick for me and spits out the following json:



[

"first": 1,
"one": "a",
"two": "b",
"last": "C1"
,

"first": 2,
"one": "d",
"two": "e",
"last": "C2"
,

"first": 3,
"one": "f",
"two": "g",
"last": "C3"
,

"first": 4,
"one": "h",
"two": "i",
"last": "C4"

]


You will need to install Newtonsoft.Json via your package manager.



using System;
using System.Collections.Generic;
using System.Linq;
using System.Text.RegularExpressions;
using Newtonsoft.Json;

namespace ConsoleApp2

class Program

class WahEvah

public int first;
public string one;
public string two;
public string last;



static void Main(string args)

List<string> myString = "1,a,b,C1,,#2,d,e,C2,,#3,f,g,C3,,#4,h,i,C4,,#".Split('#').ToList();
List<List<string>> myString2 = myString.Select(x => x.Split(',').ToList()).ToList();
List<WahEvah> l = new List<WahEvah>();
int counter = 0;
for (int i = 0; i < myString2.Count - 1; i++)

WahEvah wd = NewMethod(myString2[i], counter);
l.Add(wd);


string json = JsonConvert.SerializeObject(l, Formatting.Indented);
Console.Write(json);
Console.ReadLine();


private static WahEvah NewMethod(List<string> myString, int counter)

counter = 0;
WahEvah w = null; w = new WahEvah();
foreach (string s2 in myString)



if (counter == 0)

w.first = Convert.ToInt32(s2.Trim()); counter++; continue;

if (counter == 1)

w.one = s2.Trim(); counter++; continue;

if (counter == 2)

w.two = s2.Trim(); counter++; continue;

if (counter == 3)

w.last = s2.Trim(); counter++; continue;





return w;










share|improve this answer























  • Thanks for the inputs but I dont have a static list so cant create a fixed model as you have created.

    – user1563677
    Nov 16 '18 at 15:09











  • It's just an example. It should work with any string that follows your pattern.

    – Ole EH Dufour
    Nov 16 '18 at 15:42















0














The below code does the trick for me and spits out the following json:



[

"first": 1,
"one": "a",
"two": "b",
"last": "C1"
,

"first": 2,
"one": "d",
"two": "e",
"last": "C2"
,

"first": 3,
"one": "f",
"two": "g",
"last": "C3"
,

"first": 4,
"one": "h",
"two": "i",
"last": "C4"

]


You will need to install Newtonsoft.Json via your package manager.



using System;
using System.Collections.Generic;
using System.Linq;
using System.Text.RegularExpressions;
using Newtonsoft.Json;

namespace ConsoleApp2

class Program

class WahEvah

public int first;
public string one;
public string two;
public string last;



static void Main(string args)

List<string> myString = "1,a,b,C1,,#2,d,e,C2,,#3,f,g,C3,,#4,h,i,C4,,#".Split('#').ToList();
List<List<string>> myString2 = myString.Select(x => x.Split(',').ToList()).ToList();
List<WahEvah> l = new List<WahEvah>();
int counter = 0;
for (int i = 0; i < myString2.Count - 1; i++)

WahEvah wd = NewMethod(myString2[i], counter);
l.Add(wd);


string json = JsonConvert.SerializeObject(l, Formatting.Indented);
Console.Write(json);
Console.ReadLine();


private static WahEvah NewMethod(List<string> myString, int counter)

counter = 0;
WahEvah w = null; w = new WahEvah();
foreach (string s2 in myString)



if (counter == 0)

w.first = Convert.ToInt32(s2.Trim()); counter++; continue;

if (counter == 1)

w.one = s2.Trim(); counter++; continue;

if (counter == 2)

w.two = s2.Trim(); counter++; continue;

if (counter == 3)

w.last = s2.Trim(); counter++; continue;





return w;










share|improve this answer























  • Thanks for the inputs but I dont have a static list so cant create a fixed model as you have created.

    – user1563677
    Nov 16 '18 at 15:09











  • It's just an example. It should work with any string that follows your pattern.

    – Ole EH Dufour
    Nov 16 '18 at 15:42













0












0








0







The below code does the trick for me and spits out the following json:



[

"first": 1,
"one": "a",
"two": "b",
"last": "C1"
,

"first": 2,
"one": "d",
"two": "e",
"last": "C2"
,

"first": 3,
"one": "f",
"two": "g",
"last": "C3"
,

"first": 4,
"one": "h",
"two": "i",
"last": "C4"

]


You will need to install Newtonsoft.Json via your package manager.



using System;
using System.Collections.Generic;
using System.Linq;
using System.Text.RegularExpressions;
using Newtonsoft.Json;

namespace ConsoleApp2

class Program

class WahEvah

public int first;
public string one;
public string two;
public string last;



static void Main(string args)

List<string> myString = "1,a,b,C1,,#2,d,e,C2,,#3,f,g,C3,,#4,h,i,C4,,#".Split('#').ToList();
List<List<string>> myString2 = myString.Select(x => x.Split(',').ToList()).ToList();
List<WahEvah> l = new List<WahEvah>();
int counter = 0;
for (int i = 0; i < myString2.Count - 1; i++)

WahEvah wd = NewMethod(myString2[i], counter);
l.Add(wd);


string json = JsonConvert.SerializeObject(l, Formatting.Indented);
Console.Write(json);
Console.ReadLine();


private static WahEvah NewMethod(List<string> myString, int counter)

counter = 0;
WahEvah w = null; w = new WahEvah();
foreach (string s2 in myString)



if (counter == 0)

w.first = Convert.ToInt32(s2.Trim()); counter++; continue;

if (counter == 1)

w.one = s2.Trim(); counter++; continue;

if (counter == 2)

w.two = s2.Trim(); counter++; continue;

if (counter == 3)

w.last = s2.Trim(); counter++; continue;





return w;










share|improve this answer













The below code does the trick for me and spits out the following json:



[

"first": 1,
"one": "a",
"two": "b",
"last": "C1"
,

"first": 2,
"one": "d",
"two": "e",
"last": "C2"
,

"first": 3,
"one": "f",
"two": "g",
"last": "C3"
,

"first": 4,
"one": "h",
"two": "i",
"last": "C4"

]


You will need to install Newtonsoft.Json via your package manager.



using System;
using System.Collections.Generic;
using System.Linq;
using System.Text.RegularExpressions;
using Newtonsoft.Json;

namespace ConsoleApp2

class Program

class WahEvah

public int first;
public string one;
public string two;
public string last;



static void Main(string args)

List<string> myString = "1,a,b,C1,,#2,d,e,C2,,#3,f,g,C3,,#4,h,i,C4,,#".Split('#').ToList();
List<List<string>> myString2 = myString.Select(x => x.Split(',').ToList()).ToList();
List<WahEvah> l = new List<WahEvah>();
int counter = 0;
for (int i = 0; i < myString2.Count - 1; i++)

WahEvah wd = NewMethod(myString2[i], counter);
l.Add(wd);


string json = JsonConvert.SerializeObject(l, Formatting.Indented);
Console.Write(json);
Console.ReadLine();


private static WahEvah NewMethod(List<string> myString, int counter)

counter = 0;
WahEvah w = null; w = new WahEvah();
foreach (string s2 in myString)



if (counter == 0)

w.first = Convert.ToInt32(s2.Trim()); counter++; continue;

if (counter == 1)

w.one = s2.Trim(); counter++; continue;

if (counter == 2)

w.two = s2.Trim(); counter++; continue;

if (counter == 3)

w.last = s2.Trim(); counter++; continue;





return w;











share|improve this answer












share|improve this answer



share|improve this answer










answered Nov 15 '18 at 20:16









Ole EH DufourOle EH Dufour

1,10911121




1,10911121












  • Thanks for the inputs but I dont have a static list so cant create a fixed model as you have created.

    – user1563677
    Nov 16 '18 at 15:09











  • It's just an example. It should work with any string that follows your pattern.

    – Ole EH Dufour
    Nov 16 '18 at 15:42

















  • Thanks for the inputs but I dont have a static list so cant create a fixed model as you have created.

    – user1563677
    Nov 16 '18 at 15:09











  • It's just an example. It should work with any string that follows your pattern.

    – Ole EH Dufour
    Nov 16 '18 at 15:42
















Thanks for the inputs but I dont have a static list so cant create a fixed model as you have created.

– user1563677
Nov 16 '18 at 15:09





Thanks for the inputs but I dont have a static list so cant create a fixed model as you have created.

– user1563677
Nov 16 '18 at 15:09













It's just an example. It should work with any string that follows your pattern.

– Ole EH Dufour
Nov 16 '18 at 15:42





It's just an example. It should work with any string that follows your pattern.

– Ole EH Dufour
Nov 16 '18 at 15:42

















draft saved

draft discarded
















































Thanks for contributing an answer to Stack Overflow!


  • Please be sure to answer the question. Provide details and share your research!

But avoid


  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.

To learn more, see our tips on writing great answers.




draft saved


draft discarded














StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53326403%2fcreating-a-multidimensional-array-from-a-string-with-delimiter%23new-answer', 'question_page');

);

Post as a guest















Required, but never shown





















































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown

































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown







Popular posts from this blog

Top Tejano songwriter Luis Silva dead of heart attack at 64

政党

天津地下鉄3号線