简体   繁体   English

pythin:如何将字符串转换为 json python

[英]pythin: how do I convert string to json python

I have below data , how can I convert user data to json in python?我有以下数据,如何在 python 中将用户数据转换为 json?

results= [
            {
                "created_at": 1632712032050,
                "user": "{\"id\": \"12\", \"name\": \"test\"}"
            },
            {
                "created_at": 1632712032050,
                "user": "{\"id\": \"13\", \"name\": \"test\"}"
            }
         ]

expected output:预期输出:

results= [
            {
                "created_at": 1632712032050,
                "user": {"id": "12", "name": "test"}
            },
            {
                "created_at": 1632712032050,
                "user": {"id": "13", "name": "test"}
            }
         ]

Use the json module of python使用python的json模块

import json

results= [
            {
                "created_at": 1632712032050,
                "user": "{\"id\": \"12\", \"name\": \"test\"}"
            }
         ]

print(results) #in string format

results[0]['user'] = json.loads(results[0]['user'])

print(results) #after parsing its now json dictionary/object

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM