如何在json字符串中使用for循环来获取所有行 [英] How to use for loop in json string to get all rows

查看:90
本文介绍了如何在json字符串中使用for循环来获取所有行的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在excel列中以json格式跟随



 farm_details(excelsheet中的Coumn名称)

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



我在上面的数据显示为excel我想要输出如下



 Product1 Product2 Code1 Code2 



为了获得以上ouptut,我写了如下代码



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

foreach var 数据 in dict)
{
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(Row1)
Prodcut2:Xavin(Row2)
Code1:REO(Row3)
Code2:RM (第4行)



我在上面显示数据到excel我想要输出如下



 Prodcut1 Prodcut2 Code1 Code2 



为了获得以上ouptut我写的代码如下



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

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



当我运行上面的代码时它显示输出如下



Prodcut1(Row1 )仅显示在excel的输出中



i想要从Row1到Row4的所有行Product1 Product2 Code1 Code2



用于获得以上输出我必须做出的改变。如何使用for循环获取所有行

解决方案

  var  dict = JsonConvert.DeserializeObject< Dictionary< string,string>>(dt.Rows [ 0 ] [  farm_details]。ToString()); 



目前只从第一行获取值来自从Excel数据创建的数据表。因此,如果数据位于Excel工作表中的单独行上,则需要在使用数据时组合行或循环遍历所有行。



您可以这样做它,您需要在上下文更改时考虑到这一点,以便需要不同的json对象。换句话说,如果您在Excel中有80行,则需要考虑哪些行彼此相关以及json对象何时更改。这可能在Excel的另一列中的某些其他数据中定义...


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)
     "Prodcut2":"Xavin"  (Row2)
     "Code1":"REO"       (Row3)
     "Code2":"RM"        (Row4)


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

Prodcut1   Prodcut2    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

Prodcut1 (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

解决方案

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


At the moment you take values only from the first row from the data table which is created from the Excel data. So if the data is located on separate rows in the Excel sheet, you need to combine the rows or loop through all rows when using the data.

Either way you do it, you need to take into account when the context changes so that a different json object is required. In other words, if you for example have 80 rows in Excel you need to take into account which ones are related to each other and when the json object changes. This is perhaps defined in some other data in another column in the Excel...


这篇关于如何在json字符串中使用for循环来获取所有行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆