如何使用C#在json字符串中使用for循环 [英] How to use for loop in json string using C#
问题描述
在excel专栏中有以下json格式
farm_details(excelsheet中的Coumn名称)
Product1:Zamin(第1行)
Product2:Xavin(第2行)
Code1:REO (第3行)
Code2:RM(第4行)
我在上面的数据显示为excel我想要输出如下
产品1产品2代码1代码2
为了获得以上ouptut我写的代码如下
var dict = JsonConvert.DeserializeObject< Dictionary< string,string>>(dt.Rows [0] [farm_details]。ToString());
string str = string.Empty;
int columnindex = 43;
foreach(dict中的var数据)
{
columnindex ++;
worksheet.Cells [2,columnindex] .Value = data.Key;
}
当我运行上面的代码时它显示输出如下
Product1(Row1)仅显示在输出excel
i想要从Row1到Row4的所有行Product1 Product2 Code1 Code2
以上输出我必须做出的改变。如何使用for循环获取所有行。
我尝试过:
在excel列中以json格式跟随
farm_details(excelsheet中的Coumn名称)
Product1:Zamin(第1行)
Product2:Xavin(第2行)
Code1:REO(第3行)
Code2:RM(第4行)
我在上面显示数据到excel我想要输出如下
产品1产品2代码1代码2
为了获得以上ouptut我写的代码如下
var dict = JsonConvert.DeserializeObject< Dictionary< string,string>>(dt.Rows [0] [farm_details]。ToString());
string str = string.Empty;
int columnindex = 43;
foreach(dict中的var数据)
{
columnindex ++;
worksheet.Cells [2,columnindex] .Value = data.Key;
}
我跑的时候以上代码显示输出如下
Product1(Row1)仅显示在excel的输出中
i希望从Row1到Row4的所有行Product1 Product2 Code1 Code2
获得高于输出我必须做出的改变。如何使用for循环获取所有行。
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.
推荐答案
在C#中迭代JSON对象 - 堆栈溢出 [ ^ ]
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);
}
这篇关于如何使用C#在json字符串中使用for循环的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!