Skip to content

Commit 92c683e

Browse files
authored
Create Python如何讀取json中的數據.md
1 parent 555cda6 commit 92c683e

1 file changed

Lines changed: 51 additions & 0 deletions

File tree

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
# Python 如何讀取 json 中的數據
2+
3+
## 問題
4+
5+
```json
6+
{
7+
"errno": "0",
8+
"data": {
9+
"hi_info": {
10+
"login_ip": "127.0.0.1",
11+
"license": {
12+
"id": "123456",
13+
"name": "hello",
14+
"sex": "",
15+
"status": "1"
16+
},
17+
"hello_info": {
18+
"model": "3",
19+
"license": {
20+
"id": "123456",
21+
"license_no": ""
22+
},
23+
"upgrade": 1
24+
}
25+
}
26+
}
27+
}
28+
```
29+
30+
如何讀取自己想要的數據
31+
32+
比如:
33+
34+
* `hi_info` 中的 `login_ip`
35+
* `license` 中的 `name`
36+
* `license` 中的 `id`
37+
38+
以及 `data` 裡面所有的數據
39+
40+
問題出自 [segmentfault](), by [monkey_cici](https://segmentfault.com/u/monkey_cici)
41+
42+
## 回答
43+
44+
```python
45+
import json
46+
47+
with open('ex.json' , 'r') as reader:
48+
jf = json.loads(reader.read())
49+
50+
print(jf['data']['hi_info']['login_ip'])
51+
```

0 commit comments

Comments
 (0)