gpt4 book ai didi

c# - JToken 不包含“长度”的定义

转载 作者:太空宇宙 更新时间:2023-11-03 22:43:28 24 4
gpt4 key购买 nike

我在扩展名为 .json 的文件中有一个 JSON 数据。我从文件中抓取并读取 JSON。我的问题是如何检查 payment 数组是否有子数组。

我试过 o2["payment"].length() 但它给我一个错误

'JToken' does not contain a definition for 'length' and no extension method 'length' accepting a first argument of type 'JToken' could be found (are you missing a using directive or an assembly reference?)

我正在使用 Json.NET

{
"bank_name": "MyBank",
"payment": [
{
"ssn": "123456789",
"description": "Payment for income tax for 2015",
"amount": "55.00"
},
{
"ssn": "123456789",
"description": "Payment for income tax for 2016",
"amount": "95.10"
},
{
"ssn": "123456789",
"description": "Payment for income tax for 2017",
"amount": "35.03"
}
]
}




public ActionResult ReadFile()
{

var Path = "C:\\Payment";
var PendingPath = Path + "\\Pending";

string[] FilePaths = Directory.GetFiles(@PendingPath, "*.json");
foreach ( var CurrentFile in FilePaths )
{
using (StreamReader file = System.IO.File.OpenText(CurrentFile))
using (JsonTextReader reader = new JsonTextReader(file))
{
JObject o2 = (JObject)JToken.ReadFrom(reader);
var bank_name = o2["bank_name"];
if ( o2["payment"].length() ) //Check here if payment has any children
{

}
}
}

return new EmptyResult();
}

最佳答案

如果你想使用数组的方法,你应该转换对象:

if (((JArray)o2["payment"]).Count > 0)
{
// Logic...
}

关于c# - JToken 不包含“长度”的定义,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51050923/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com