SoFunction
Updated on 2024-11-14

Python processing json string into a simple implementation of the dictionary conversion

A friend gave a need today:
come and go

{'isOK': 1, 'isRunning': None, 'isError': None}

How to convert to dictionary

Well, at first glance the json conversion is very simple to start:

import json

a = "{'isOK': 1, 'isRunning': None, 'isError': None}"

print (a) 

Dead or alive can not come out of the results, but also reported an error, check two hours of Baidu, did not understand.

Finally, directly copy the online code, OK, run successfully, but fill in my a variable, no, error; began to compare the two variables what is the difference between the two, always thought Python double quotes single quotes can be used casually, the meaning remains unchanged; finally Baidu know the standard format of json:Only double quotes can be used for key or value delimiters, not single quotes, and "keys" must be delimited (double quotes).
Changed it and it's OK.

#-*-coding:utf-8-*-

import json

a = '{"isOK": 1, "isRunning": "None", "isError": "None"}'

b = (a)

print b["isOK"] 

Maybe other languages like php,js can recognize non-standard json format, but not Python;

Question: how does python convert a variable like a to dict? Provided the data is large .....

Above this Python processing json string into a dictionary is a simple implementation of all the content that I have shared with you, I hope to be able to give you a reference, but also hope that you support me more.