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

如何通过Key获取JSON中的数组?以Key"15400001"提取数组为例

Hey there! Let's tackle your two JSON-related questions clearly and practically, since I know digging into nested data can feel tricky at first.

1. General Approach: Fetch an Array from JSON Using a Key

First, remember that raw JSON is just a string—you'll need to parse it into your programming language's native data structure (like a dictionary/list in Python, or object/array in JavaScript) before you can access the data. Once parsed, follow these steps:

  • Map out the hierarchy: Figure out where your target key lives. Is it directly in a top-level object, or nested inside other objects/arrays?
  • Access the key directly: If the key is in a top-level object, use object[key] (works in most languages). For nested structures, chain the access—e.g., outer_object["nested_key"]["target_key"].
  • Handle missing keys safely: To avoid runtime errors if the key doesn't exist, use language-specific safety features:
    • Python: Use the dict.get() method (e.g., object.get("target_key", default_value) to return a fallback if the key is missing).
    • JavaScript: Use the optional chain operator ?. (e.g., object?.["target_key"]) and pair it with a default value if needed.
2. Extracting the Array from Your Specific Data

Your given data is an array containing a single object, with the key "15400001" mapping to the array you want. Here's how to pull it out in two common languages:

Python Example

data = [{"15400001": [0, 7, 603, 607, 1655, 1661, 1703, 1706, 1823, 1825, 1826, 1827, 2423, 2471, 2475, 2477]}]

# Direct access (simple, but throws an error if the key/index is missing)
target_array = data[0]["15400001"]
print(target_array)

# Safe access with get() (returns an empty array if the key doesn't exist)
target_array_safe = data[0].get("15400001", [])
print(target_array_safe)

JavaScript Example

const data = [{"15400001": [0, 7, 603, 607, 1655, 1661, 1703, 1706, 1823, 1825, 1826, 1827, 2423, 2471, 2475, 2477]}];

// Direct access
const targetArray = data[0]["15400001"];
console.log(targetArray);

// Safe access with optional chain
const targetArraySafe = data[0]?.["15400001"] || [];
console.log(targetArraySafe);

Quick breakdown: Since data is an array, we first grab the first element with data[0] (arrays are zero-indexed). Then we access the key "15400001" on that object to get the array.

内容的提问来源于stack exchange,提问作者UYAR.C

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.05.13 09:27:56