# Python 读取 JSON 值的方法

**URL:** <https://www.isharkfly.com/t/python-json/16580>\
**Category:** Python\
**Tags:** python\
**Created:** [November 6, 2024, 10:57pm UTC](https://www.isharkfly.com/t/python-json/16580 "2024-11-06T22:57:56Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![honeymoose](https://www.isharkfly.com/user_avatar/www.isharkfly.com/honeymoose/32/20796_2.png) [@honeymoose](https://www.isharkfly.com/u/honeymoose)\
**Post date:** [November 6, 2024, 10:57pm UTC](https://www.isharkfly.com/t/python-json/16580/1 "2024-11-06T22:57:56Z")

</div>

python json read value

* * *

如果 Python 使用的嵌套方式进行读取的话，可以使用下面的方法进行读取：

```auto
json_string = '{"person": {"name": "Alice", "address": {"street": "Main St"}}}'
data = json.loads(json_string)

print(data["person"]["address"]["street"])

```

---

<div class="post-metadata">

**Author:** ![gpt4o\_bot](https://www.isharkfly.com/user_avatar/www.isharkfly.com/gpt4o_bot/32/15773_2.png) [@gpt4o\_bot](https://www.isharkfly.com/u/gpt4o_bot)\
**Post date:** [November 6, 2024, 10:57pm UTC](https://www.isharkfly.com/t/python-json/16580/2 "2024-11-06T22:57:56Z")

</div>

To read a value from a JSON object in Python, you can use the `json` library to parse JSON data and access its values using standard Python dictionary operations. Below is an example of how you can do this:

```python
import json

# Sample JSON data as a string
json_data = '{"name": "John Doe", "age": 30, "city": "New York"}'

# Parse the JSON data
data = json.loads(json_data)

# Access values
name = data['name']
age = data['age']
city = data['city']

print(f'Name: {name}')
print(f'Age: {age}')
print(f'City: {city}')

```

Here’s a step-by-step explanation:

1. **Import the `json` Library** : This library provides functionalities to work with JSON data.
2. **Parse JSON Data** : Use `json.loads()` to convert a JSON-formatted string into a Python dictionary.
3. **Access Dictionary Values** : Once you have the JSON data in a dictionary, you can access values using keys, just like any other Python dictionary.

If you are dealing with a JSON file, you can read the file first and then parse the JSON:

```python
import json

# Assume we have a file named 'data.json'
with open('data.json', 'r') as file:
    data = json.load(file) # This automatically parses the JSON
    
# Access values
name = data['name']
age = data['age']
city = data['city']

print(f'Name: {name}')
print(f'Age: {age}')
print(f'City: {city}')

```

In this case, `json.load()` directly reads and parses the JSON data from a file.

---

<div class="post-metadata">

**Author:** ![honeymoose](https://www.isharkfly.com/user_avatar/www.isharkfly.com/honeymoose/32/20796_2.png) [@honeymoose](https://www.isharkfly.com/u/honeymoose)\
**Post date:** [November 6, 2024, 10:58pm UTC](https://www.isharkfly.com/t/python-json/16580/3 "2024-11-06T22:58:30Z")

</div>



---

<div class="post-metadata">

**Author:** ![honeymoose](https://www.isharkfly.com/user_avatar/www.isharkfly.com/honeymoose/32/20796_2.png) [@honeymoose](https://www.isharkfly.com/u/honeymoose)\
**Post date:** [November 6, 2024, 11:02pm UTC](https://www.isharkfly.com/t/python-json/16580/4 "2024-11-06T23:02:10Z")

</div>

> [@gpt4o\_bot](#):
>
> json.load()

json.load() 的这个方法可以读取文件，也可以读取字符串。

通常这是对 Json 数据处理的第一步。
