英文:
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:
{
"majorDimension": "ROWS",
"range": "Sheet1!A2:H1000",
"values": [
[
"Test1",
"20/07/2023",
"21/07/2023"
],
[
"Test2",
"10/08/2023",
"12/08/2023"
]
],
"ETag": 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, "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 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 = "
{\"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());
}
}
答案2
得分: 0
以下是已翻译的部分:
如@orhtej2所指出,您缺少了一个[i]。 代码应该是
for (int i = 0; i < sheetResponse.values.Count; i++)
{
for (int j = 0; j < 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 < sheetResponse.values.Count; i++)
{
for (int j = 0; j < sheetResponse.values[i].Count; j++)
{
Console.WriteLine(sheetResponse.values[i][j]);
}
}
I am voting to close this as a typo.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论