java - 使用 Groovy 计算 JSON 对象中的项目
问题描述
我的 JSON 文件
{
"General": [
{
"AppUrl": "abc.com/",
"HTTPResponseCode": "200",
"QA Build Version#": "4.3.44"
}
]
}
我想获取上述 JSON 对象的计数元素(键值对)。
我尝试使用下面的代码,但它返回 acount
为 1。
我的代码
def InputJSON = new JsonSlurper().parse(new File(fileName))
def count = InputJSON.General.size()
解决方案
代替:
def InputJSON = new JsonSlurper().parse(new File(fileName))
def count = InputJSON.General.size()
应该:
def InputJSON = new JsonSlurper().parse(new File(fileName))
def count = InputJSON.General.keySet().size()
推荐阅读
- python - Sklearn 错误:“AttributeError:‘MissingValues’对象没有属性‘to_list’”
- amazon-web-services - AWS Cloudwatch = 按 EventID 过滤事件
- python - 使用 joblib.load 从磁盘读取 xgboost 模型时出现类型错误
- pdf - 您可以在不“签名”的情况下向 PDF 添加时间戳不可篡改吗?
- python - 如何将一列字符串转换为单个字符的列?蟒蛇熊猫
- javascript - Opera 插件 windows.create 忽略 createData 维度和位置属性
- python - 如何使用 python 从 x509 获取公钥 sha 256 哈希
- javascript - 如何使用`object-fit:contain`获取图像像素坐标
- c++ - 静态成员变量只能在类的静态方法中使用,但为什么我们可以在 C++ 类的构造函数中为静态变量赋值呢?
- reactjs - 在 gatsby-ssr 文件中,我没有得到头盔的空值,并且 setHeadComponents 没有正确设置它的优先级