The difference between python-dictionary and Json

In Python, JSON (JavaScript Object Notation) and dictionary (dictionary) are two different data structures, and they have the following differences:

  1. Format:

    1. JSON is a text format for storing and exchanging data, which uses key-value pairs and lists to represent data. JSON has fixed grammatical rules, such as using double quotes to represent strings, curly braces to represent objects (a collection of key-value pairs), and square brackets to represent arrays (ordered collections of values)

    2. A dictionary is a built-in data type in Python that represents data in the form of key-value pairs.

  2. use:

    1. JSON is mainly used for serialization and deserialization of data, and is usually used for data transmission and storage. It can convert complex data structures into strings and enable data exchange between different programming languages

    2. Dictionaries are widely used in Python to store, manipulate and manage data, and it provides a wealth of methods and functions for working with key-value pair data

  3. compatibility:

    1. JSON is a universal data format that can be widely supported by different programming languages ​​and platforms

    2. Dictionaries are a unique data type of Python, which may not have a direct corresponding concept in other programming languages

  4. Orderliness:

    1. Arrays in JSON are ordered, preserving the insertion order of elements

    2. While dictionaries are unordered, it does not guarantee the insertion order of elements

  5. string key:

    1. Keys in JSON must be strings

    2. The keys in the dictionary can be of any immutable type (such as strings, integers, floating point numbers, etc.), but cannot be mutable types (such as lists, dictionaries)

To sum up, JSON is a text format for data exchange and storage, with fixed grammatical rules, and compatible with different programming languages. A dictionary is a built-in data type in Python for storing and manipulating key-value pair data, and it has rich methods and functions. JSON is more general and cross-platform, while dictionaries are more flexible and easy to use in Python

Convert between Json and dict (dictionary)

Translation: load: load dump: dump

json to dict

import json

res=json.loads('{"name":"jingbeng"}')
print(res)
print(type(res))

output:

{'name': 'jingbeng'}
<class 'dict'>

dict to json

import json

dict1={"name":"jingbeng"}
json_data=json.dumps(dict1)
print(json_data)
print(type(json_data))

output:

{"name": "jingbeng"}
<class 'str'>

Note (all k:v written in pycharm are dict types whether they are pure single quotes or double quotes or single and double mixed, and the k:v of json must be enclosed by "")

for example:

json_data1={'name':'jingebng'}
json_data2={"name":"jingbeng"}
json_data3={'name':"jingbeng"}
json_data4={"name":'jingbeng'}

print(type(json_data1))
print(type(json_data2))
print(type(json_data3))
print(type(json_data4))

json_data5={
    "name":"jingbeng"
}
print(type(json_data5))

output:

<class 'dict'>
<class 'dict'>
<class 'dict'>
<class 'dict'>
<class 'dict'>

So generally to get the data type of json, use json.dumps(dict_data) conversion

If the dictionary is k:v written in single quotes, then when the dictionary is converted into jison (string), k:v will become double quotes

dict_data={'name':'jingbrng'}
dict_data2={'name':'jingbeng'}
dict_data3={'name':"jingbneg"}
print(json.dumps(dict_data))
print(json.dumps(dict_data2))
print(json.dumps(dict_data3))

output:

{"name": "jingbrng"}
{"name": "jingbeng"}
{"name": "jingbneg"}

Converting json to dict becomes '' single quotes enclosing k:v

json_data='{"name":"jingeng"}'
print(type(json_data))
dict_data=json.loads(json_data)
print(dict_data)
print(type(dict_data))

output:

<class 'str'>
{'name': 'jingeng'}
<class 'dict'>

Guess you like

Origin blog.csdn.net/weixin_53328532/article/details/131604507