The two structures of json and the way that json files are converted to python's dict

1. Two structures of json:

1. Object: The object is represented as the content enclosed by "{ }" in json. The data structure is the key-value pair structure {key:value, key:value}. In object-oriented languages, the key is the attribute of the object, and the value is The corresponding attribute value, . The value method is to obtain the property value of the object.key. The type of this property value can be number, string, array, or object. Braces {} are used to describe a set of "unordered key-value pairs of different types, that is, there is no clear relationship between each key.
The following is an example of the object structure of json:

{"firstName":"Brett","lastName":"McLaughlin","email":"aaaa"}
每个属性和值之间用":"分隔
不同属性之间用","分隔

In json, the key name of an object must be enclosed in double quotes; two properties with the same name should not appear in the same object.

2. Array: In json, the array is the content enclosed in [ ], and the data structure is { item1, item2, ..., itemn} where item is in the form of key-value key-value pairs. The value method is the same as that of the array. Index acquisition, where the type of attribute value can be: number, string, array, object (here it is implied that the array [ ] can be foreground object { }). Square brackets [] are used to describe a set of "ordered sets of data of the same type"

{
    "people":[
        {"firstName":"Brett","lastName":"McLaughlin","email":"aaaa"},
        {"firstName":"Jason","lastName":"Hunter","email":"bbbb"},
        {"firstName":"Elliotte","lastName":"Harold","email":"cccc"}
    ]
}

数组值之间用","分隔
访问形式为:people[0].firstname
输出:Brett

In this example the people array contains three values ​​which are again json objects.
"," cannot be added after the last member of arrays and objects. Through the two structures of objects and arrays, complex data structures can be combined.
Reference URL: http://www.runoob.com/json/json-syntax.html

2. Python's dict structure:

Dictionaries are another mutable container model and can store objects of any type.
Each key-value (key=>value) pair of the dictionary is separated by a colon (:), and each pair is separated by a comma (,). The entire dictionary is enclosed in curly brackets ({}), and the format is as follows:

dict = {'Name': 'Zara', 'Age': 7, 'Name': 'Manni'};

.Attention
1. The value of the python dictionary can go to any object, which can be either a standard python object or a user-defined object, but the key of dict does not work.
2. The same key is not allowed to appear twice. If the same key is assigned twice during creation, the latter value will be remembered.
3. The key must be immutable, so it can be used as a number, string or tuple, so a list will not work.

3. Serialization and deserialization:

Serialization : The process of converting the state information of an object (such as python's simple data types: list, dict, tuple, int, float, unicode) into storable or transferable content (such as json, xml).
Deserialization : Read the object state that needs to be deserialized from the storage file or storage area (such as json, xml), and rebuild the object.
json : (javascript object notation) a lightweight data exchange format that is simpler, easier to read, write, parse and generate than xml, json is a subset of javascript

The json module is integrated in python:
serialization --encoding: encode a python object into a json string (note: this refers to a json string, the implication is that a non-json string file can be converted into a json string, and then then deserialize)
deserialize --decoding: decode a string into a python object

4. Serialization and deserialization:

Serialization method : (contrast map of Python object conversion to json)

write picture description here
For each element in the python object, the python dictionary will be converted into a json object, the python list and tuple will be converted into an array, and so on.
Deserialization : (contrast map of json object converted to Python object)

write picture description here
For each element in the json string, the string will be converted to python's str, the object will be converted to python's dict, the array will be converted to python's list and so on

Reference URL: https://docs.python.org/3/library/json.html

5. Here is an example:

dumps is to convert dict to str format, loads is to convert str to dict format.

dump and load are similar functions, but combined with file operations.

The json file is as follows:

{
    "dataset": {
        "train": {
            "type": "mnist",
            "data_set": "train",
            "layout_x": "tensor"
        },
        "test": {
            "type": "mnist",
            "data_set": "test",
            "layout_x": "tensor"
        }
    },
    "train": {
        "keep_model_in_mem": 0,
        "random_state": 0,
        "data_cache": {
            "cache_in_disk": {
                "default": 1
            },
            "keep_in_mem": {
                "default": 0
            },
            "cache_dir": "/mnt/raid/fengji/gcforest/mnist/fg-tree500-depth100-3folds/datas"
        }
    },
     "outputs": ["pool1/7x7/ets", "pool1/7x7/rf", "pool1/10x10/ets", "pool1/10x10/rf", "pool1/13x13/ets", "pool1/13x13/rf"],
     "estimators": [
            {"n_folds":3,"type":"ExtraTreesClassifier","n_estimators":500,"max_depth":100,"n_jobs":-1,"min_samples_leaf":10},
            {"n_folds":3,"type":"RandomForestClassifier","n_estimators":500,"max_depth":100,"n_jobs":-1,"min_samples_leaf":10}
        ]
}

The python object is as follows:

dictObject={
    "dataset": {
        "train": {
            "type": "mnist",
            "data_set": "train",
            "layout_x": "tensor"
        },
        "test": {
            "type": "mnist",
            "data_set": "test",
            "layout_x": "tensor"
        }
    },
    "train": {
        "keep_model_in_mem": 0,
        "random_state": 0,
        "data_cache": {
            "cache_in_disk": {
                "default": 1
            },
            "keep_in_mem": {
                "default": 0
            },
            "cache_dir": "/mnt/raid/fengji/gcforest/mnist/fg-tree500-depth100-3folds/datas"
        }
    },
     "outputs": ["pool1/7x7/ets", "pool1/7x7/rf", "pool1/10x10/ets", "pool1/10x10/rf", "pool1/13x13/ets", "pool1/13x13/rf"],
     "estimators": [
            {"n_folds":3,"type":"ExtraTreesClassifier","n_estimators":500,"max_depth":100,"n_jobs":-1,"min_samples_leaf":10},
            {"n_folds":3,"type":"RandomForestClassifier","n_estimators":500,"max_depth":100,"n_jobs":-1,"min_samples_leaf":10}
        ]
}

Deserialization: json --> python

# 读取json数据的代码:
path="D:\PycharmProjects/untitled\jsonformat"
with open(path) as f:
    deserialization=json.load(f)
print(type(deserialization))
print(type(deserialization["train"]))
print(type(deserialization["outputs"]))
print(type(deserialization["estimators"]))
print(type(deserialization["dataset"]["train"]["type"]))
print(type(deserialization["train"]["keep_model_in_mem"]))
print(type(deserialization["train"]["data_cache"]))

# 对应的输出:
<class 'dict'>
<class 'dict'>
<class 'list'>
<class 'list'>
<class 'str'>
<class 'int'>
<class 'dict'>
# 与json--->python的对照图一致

Serialization: python --> json

# 读取python对象的代码:

serialization=json.dumps(dictObject)
print(type(serialization))
print(serialization)

# 输出结果:
<class 'str'>
{"dataset": {"train": {"type": "mnist", "layout_x": "tensor", "data_set": "train"}, "test": {"type": "mnist", "layout_x": "tensor", "data_set": "test"}}, "train": {"random_state": 0, "data_cache": {"cache_in_disk": {"default": 1}, "cache_dir": "/mnt/raid/fengji/gcforest/mnist/fg-tree500-depth100-3folds/datas", "keep_in_mem": {"default": 0}}, "keep_model_in_mem": 0}, "outputs": ["pool1/7x7/ets", "pool1/7x7/rf", "pool1/10x10/ets", "pool1/10x10/rf", "pool1/13x13/ets", "pool1/13x13/rf"], "estimators": [{"type": "ExtraTreesClassifier", "n_jobs": -1, "n_estimators": 500, "max_depth": 100, "n_folds": 3, "min_samples_leaf": 10}, {"type": "RandomForestClassifier", "n_jobs": -1, "n_estimators": 500, "max_depth": 100, "n_folds": 3, "min_samples_leaf": 10}]}

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325987773&siteId=291194637