织梦CMS - 轻松建站从此开始!

欧博ABG官网-欧博官方网址-会员登入

欧博abgJSON Lists with Examples

时间:2025-11-10 02:01来源: 作者:admin 点击: 1 次
Learn JSON list structure, syntax, examples, and Python code for parsing and processing JSON lists. Detailed explanations for beginners and profession

Comprehensive Guide to JSON Lists with Examples Last update on November 07 2025 12:16:09 (UTC/GMT +8 hours)

Understanding JSON Lists with Examples and Code Explained

JSON (JavaScript Object Notation) is a lightweight data-interchange format, easy for humans to read and write and simple for machines to parse and generate. A JSON list is essentially an array structure that holds multiple JSON objects or values.


Syntax of JSON List:

[ { "key1": "value1", "key2": "value2" }, { "key1": "value3", "key2": "value4" } ]

A JSON list is enclosed in square brackets [].

Each item in the list can be a JSON object (enclosed in curly braces {}) or a simple value (e.g., string, number, boolean).


Example JSON List with Code and Explanation

Here’s an example to parse and use a JSON list in Python:

JSON List Example:

[ {"id": 1, "name": "Sara", "age": 25}, {"id": 2, "name": "Bob", "age": 30}, {"id": 3, "name": "Charlie", "age": 35} ]

Python Code to Process JSON List:

Code:

# Import the json module to work with JSON data import json # Define a JSON list as a string json_data = ''' [ {"id": 1, "name": "Sara", "age": 25}, {"id": 2, "name": "Bob", "age": 30}, {"id": 3, "name": "Charlie", "age": 35} ] ''' # Parse the JSON string into a Python list data = json.loads(json_data) # Convert JSON string to Python objects # Iterate over each item in the JSON list for item in data: # Print each person's details print(f"ID: {item['id']}, Name: {item['name']}, Age: {item['age']}")

Output:

ID: 1, Name: Sara, Age: 25 ID: 2, Name: Bob, Age: 30 ID: 3, Name: Charlie, Age: 35

Code Explanation:

1. Importing the json Module:

Python’s built-in json module allows you to parse JSON data into Python objects.

2. Defining JSON Data:

The JSON list is defined as a multi-line string using triple quotes.

3. Parsing JSON String:

The json.loads() function parses the JSON string and converts it into a Python list of dictionaries.

4. Iterating Through the List:

A for loop is used to iterate through the list.

Each dictionary in the list is accessed, and its keys (id, name, age) are used to retrieve values.

5. Printing Data:

The details of each person (ID, name, age) are displayed.


Additional Notes

Accessing Specific Elements: Use indexing to access specific items:

print(data[0]['name']) # Output: Sara

Modifying JSON Data: Add or update elements dynamically:

data[0]['age'] = 26 # Update Sara's age data.append({"id": 4, "name": "Diana", "age": 28}) # Add a new person

Dumping JSON: Convert Python objects back to a JSON string:

json_output = json.dumps(data, indent=4) print(json_output)

Practical Guides to JSON Snippets and Examples.



Follow us on Facebook and Twitter for latest update.

Load Disqus Comments

(责任编辑:)
------分隔线----------------------------
发表评论
请自觉遵守互联网相关的政策法规,严禁发布色情、暴力、反动的言论。
评价:
表情:
用户名: 验证码:
发布者资料
查看详细资料 发送留言 加为好友 用户等级: 注册时间:2025-11-10 08:11 最后登录:2025-11-10 08:11
栏目列表
推荐内容