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

如何解析JSON数组?如何获取日期数组内的时间值?已解析至'2'

1. Parsing a JSON Array

Parsing a JSON array varies slightly by programming language, but here are practical examples for two widely used languages:

JavaScript

Start by converting the JSON string to a native array using JSON.parse(), then iterate over it just like any regular array:

// Example JSON array string
const jsonArrayStr = '[{"event": "concert"}, {"event": "workshop"}]';
const parsedArray = JSON.parse(jsonArrayStr);

// Loop through the parsed array
parsedArray.forEach(item => {
  console.log(`Event type: ${item.event}`);
});

Python

Use the built-in json module to parse the JSON string into a Python list, then loop through its elements:

import json

# Example JSON array string
json_array_str = '["morning", "afternoon", "evening"]'
parsed_array = json.loads(json_array_str)

for period in parsed_array:
    print(f"Time period: {period}")

2. Extracting Time Values from Your Specific JSON Structure

Since you can already access the value under the key '2', the next step is to iterate through the date keys in that object, then pull out each array of times. Here's how to do it in two common languages:

JavaScript

Assuming you’ve parsed the full JSON into a variable called data:

// Access the '2' object you mentioned
const dateTimesObj = data.date_times['2'];

// Loop through each date key in the object
for (const date in dateTimesObj) {
  const timesArray = dateTimesObj[date];
  // Iterate through each time in the array for this date
  timesArray.forEach(time => {
    console.log(`Found time: ${time} on ${date}`);
    // Add your logic to use the time value here
  });
}

Python

After parsing the JSON and accessing the '2' object:

import json

// Example full JSON string
json_str = '''{
  "date_times": { 
    "2": { 
      "2018-01-08": [ "19:00:00" ], 
      "2018-01-09": [ "13:30:00", "19:00:00" ], 
      "2018-01-10": [ "13:30:00", "19:00:00" ], 
      "2018-01-11": [ "13:30:00", "19:00:00" ] 
    } 
  }
}'''

data = json.loads(json_str)
date_times_obj = data['date_times']['2']

// Iterate over dates and their corresponding time arrays
for date, times in date_times_obj.items():
    for time in times:
        print(f"Time: {time} (Date: {date})")
        // Process each time value here as needed

The core logic translates to any language: loop through the date keys in the '2' object, then access and process each element in the time array tied to that date.

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.05.15 07:39:45