You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何访问Dictionary中Object的Inner Array?获取label值与instances长度

JSON数据操作:访问label取值与统计instances长度

你的JSON是包含单个对象的数组,操作时需先定位到数组的第一个元素(索引为0),再对目标属性进行操作。以下是常用编程语言的实现示例:

一、访问label的取值

JavaScript 实现

// 假设已解析得到JSON对象jsonData
const jsonData = [
    {
        "_id": "62b2ebff955fe1001d225781",
        "datasetName": "comments",
        "action": "dataset",
        "comment": "Initial data!",
        "instances": ["62b2eb94955fe1001d22576a", "62b2eba1955fe1001d22576e", "62b2eba9955fe1001d225770", "62b2ebb9955fe1001d225772", "62b2ebcc955fe1001d225774", "62b2ebe2955fe1001d225778"],
        "label": ["Contemporary", "Tango"]
    }
];

// 获取完整的label数组
const allLabels = jsonData[0].label;
console.log(allLabels); // 输出: ["Contemporary", "Tango"]

// 获取单个label(比如第一个)
const firstLabel = jsonData[0].label[0];
console.log(firstLabel); // 输出: "Contemporary"

// 遍历所有label
jsonData[0].label.forEach(label => console.log(label));

Python 实现

import json

# 从文件加载并解析JSON(也可直接使用已解析的对象)
with open("your_dataset.json", "r") as file:
    json_data = json.load(file)

# 获取完整的label数组
all_labels = json_data[0]["label"]
print(all_labels) # 输出: ['Contemporary', 'Tango']

# 获取单个label(比如第二个)
second_label = json_data[0]["label"][1]
print(second_label) # 输出: 'Tango'

# 遍历所有label
for label in json_data[0]["label"]:
    print(label)

二、统计instances数组的长度

JavaScript 实现

// 直接通过length属性获取长度
const instancesCount = jsonData[0].instances.length;
console.log(instancesCount); // 输出: 6

Python 实现

// 通过len()函数获取数组长度
instances_count = len(json_data[0]["instances"])
print(instances_count) # 输出: 6

内容的提问来源于stack exchange,提问作者Behnoosh Mzd

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.25 19:06:26