如何在C#中迭代嵌套数组?

huangapple go评论65阅读模式
英文:

How to iterate through nested array in C#?

问题

我有一个控制台的C#应用程序,我使用Google Sheets API来从电子表格中获取数据。API返回给我的数据如下:

{
    "majorDimension": "ROWS",
    "range": "Sheet1!A2:H1000",
    "values": [
        [
            "Test1",
            "20/07/2023",
            "21/07/2023"
        ],
        [
            "Test2",
            "10/08/2023",
            "12/08/2023"
        ]
    ],
    "ETag": null
}

这些 "values" 实际上是单元格中的值,我需要循环遍历这些值以提取 "Test" 和 "Test2" 的值。有谁知道如何做到这一点?

var sheetRequest = sheetService.Spreadsheets.Values.Get(spreadsheetId, "A2:H");

var sheetResponse = await sheetRequest.ExecuteAsync();

for (int i = 0; i < sheetResponse.Values.Count; i++)
{
    for (int j = 0; j < sheetResponse.Values.Count; j++)
    {
        string s = (string)sheetResponse.Values[i][j];
        Console.WriteLine(s);
    }
}

我尝试了这个。我不知道如何访问元素?有什么问题吗?

英文:

I have a console C# app and I use Google Sheets API to get data from spreadsheet. API returns me the following:

{
    &quot;majorDimension&quot;: &quot;ROWS&quot;,
    &quot;range&quot;: &quot;Sheet1!A2:H1000&quot;,
    &quot;values&quot;: [
        [
            &quot;Test1&quot;,
            &quot;20/07/2023&quot;,
            &quot;21/07/2023&quot;
        ],
        [
            &quot;Test2&quot;,
            &quot;10/08/2023&quot;,
            &quot;12/08/2023&quot;
        ]
    ],
    &quot;ETag&quot;: null
}

These "values" are actually the values from the cells, and I need to loop through those values to extract the values "Test" and "Test2". Does anyone know how to do this?

var sheetRequest = sheetService.Spreadsheets.Values.Get(spreadsheetId, &quot;A2:H&quot;);

var sheetResponse = await sheetRequest.ExecuteAsync();

for (int i = 0; i &lt; sheetResponse.Values.Count; i++)
{
    for (int j = 0; j &lt; sheetResponse.Values.Count; j++)
    {
        string s = (string)sheetResponse.Values[i][j];
        Console.WriteLine(s);
    }
}

I tried this. I don't know how to access elements? What is wrong?

答案1

得分: 1

请尝试这样做。

using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
using System;

// Your JSON string
string jsonString = @"
{
    ""majorDimension"": ""ROWS"",
    ""range"": ""Sheet1!A2:H1000"",
    ""values"": [[""Test1"", ""20/07/2023"", ""21/07/2023""],[""Test2"", ""10/08/2023"", ""12/08/2023""]],
    ""ETag"": null
}";

// Parse the JSON string into a JObject
JObject jsonObject = JObject.Parse(jsonString);

// Access the ""values"" array
JArray valuesArray = (JArray)jsonObject[""values""];

// Iterate over the ""values"" array
foreach (JArray innerArray in valuesArray)
{
    foreach (JToken token in innerArray)
    {
        Console.WriteLine(token.ToString());
    }
}

希望这对你有所帮助。

英文:

please try this.

using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
using System;

// Your JSON string
string jsonString = &quot; 
{\&quot;majorDimension\&quot;:\&quot;ROWS\&quot;,\&quot;range\&quot;:\&quot;Sheet1!A2:H1000\&quot;,\&quot;values\&quot;:[[\&quot;Test1\&quot;,\&quot;20/07/2023\&quot;,\&quot;21/07/2023\&quot;],[\&quot;Test2\&quot;,\&quot;10/08/2023\&quot;,\&quot;12/08/2023\&quot;]],\&quot;ETag\&quot;:null}&quot;;

// Parse the JSON string into a JObject
JObject jsonObject = JObject.Parse(jsonString);

// Access the &quot;values&quot; array
JArray valuesArray = (JArray)jsonObject[&quot;values&quot;];

// Iterate over the &quot;values&quot; array
foreach (JArray innerArray in valuesArray)
{
    foreach (JToken token in innerArray)
    {
        Console.WriteLine(token.ToString());
    }
}

答案2

得分: 0

以下是已翻译的部分:

如@orhtej2所指出,您缺少了一个[i]。 代码应该是

for (int i = 0; i &lt; sheetResponse.values.Count; i++)
{
    for (int j = 0; j &lt; sheetResponse.values[i].Count; j++)
    {
        Console.WriteLine(sheetResponse.values[i][j]);
    }
}

我投票关闭此问题,认为这是一个拼写错误。

英文:

As @orhtej2 has pointed out, you are missing a [i]. The code should be

for (int i = 0; i &lt; sheetResponse.values.Count; i++)
{
    for (int j = 0; j &lt; sheetResponse.values[i].Count; j++)
    {
        Console.WriteLine(sheetResponse.values[i][j]);
    }
}

I am voting to close this as a typo.

huangapple
  • 本文由 发表于 2023年5月29日 19:22:03
  • 转载请务必保留本文链接:https://go.coder-hub.com/76356904.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定