Click here to Skip to main content
15,893,486 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
In excel column has follows in json format

farm_details (Coumn name in excelsheet)

"Product1":"Zamin" (Row1)
"Product2":"Xavin" (Row2)
"Code1":"REO" (Row3)
"Code2":"RM" (Row4)

I am displaying above data in to excel i want output as follows

Product1 Product2 Code1 Code2

For getting above ouptut i written the code as follows

var dict = JsonConvert.DeserializeObject<Dictionary<string, string>>(dt.Rows[0]["farm_details"].ToString());
string str = string.Empty;
int columnindex = 43;

foreach (var data in dict)
{
columnindex++;
worksheet.Cells[2, columnindex].Value = data.Key;
}

when i run the above code it shows output as follows

Product1 (Row1) only shows in the ouput in excel

i want all the Row from Row1 to Row4 Product1 Product2 Code1 Code2

for getting above output what changes i have to made. how to use for loop to get all the row.

What I have tried:

In excel column has follows in json format

farm_details (Coumn name in excelsheet)

"Product1":"Zamin" (Row1)
"Product2":"Xavin" (Row2)
"Code1":"REO" (Row3)
"Code2":"RM" (Row4)

I am displaying above data in to excel i want output as follows

Product1 Product2 Code1 Code2

For getting above ouptut i written the code as follows

var dict = JsonConvert.DeserializeObject<Dictionary<string, string>>(dt.Rows[0]["farm_details"].ToString());
string str = string.Empty;
int columnindex = 43;

foreach (var data in dict)
{
columnindex++;
worksheet.Cells[2, columnindex].Value = data.Key;
}

when i run the above code it shows output as follows

Product1 (Row1) only shows in the ouput in excel

i want all the Row from Row1 to Row4 Product1 Product2 Code1 Code2

for getting above output what changes i have to made. how to use for loop to get all the row.
Posted
Updated 2-May-18 19:59pm
Comments
Patrice T 2-May-18 21:58pm    
160+ questions and still Reposting !

1 solution

Iterating over JSON object in C# - Stack Overflow[^]

<pre lang="c#">dynamic dynJson = JsonConvert.DeserializeObject(json);
foreach (var item in dynJson)
{
    Console.WriteLine("{0} {1} {2} {3}\n", item.id, item.displayName, 
        item.slug, item.imageUrl);
}
 
Share this answer
 
Comments
[no name] 3-May-18 2:34am    
From my above how to do

var dict = JsonConvert.DeserializeObject<Dictionary<string, string>>(dt.Rows[0]["farm_details"].ToString());
string str = string.Empty;
int columnindex = 43;

foreach (var data in dict)
{
columnindex++;
worksheet.Cells[2, columnindex].Value = data.Key;
}

please let me know

becuase i am new to json concept. please help me.

from my abvoe code what changes i have to made.
[no name] 3-May-18 4:04am    
please let me know

ar dict = JsonConvert.DeserializeObject<Dictionary<string, string>>(dt.Rows[0]["farm_details"].ToString());
string str = string.Empty;
int columnindex = 43;

foreach (var data in dict)
{
columnindex++;
worksheet.Cells[2, columnindex].Value = data.Key;
}

please let me know

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900