str在python中进行dict,但保持json属性的序列

2024-04-20 10:05:22 发布

您现在位置:Python中文网/ 问答频道 /正文

我试过了评估以及json.loads文件但这两种方法在提供字符串时都不维护json属性的序列。请看下面的例子-

在提供给json.loads文件-你知道吗

{
    "type": "array",
    "properties": {
        "name": {
            "type": "string"
        },
        "i": {
            "type": "integer"
        },
        "strList": {
            "type": "array",
            "items": {
                "type": "string"
            }
        },
        "strMap": {
            "type": "object"
        },
        "p2": {
            "type": "array",
            "items": {
                "type": "object",
                "properties": {
                    "name": {
                        "type": "string"
                    },
                    "i": {
                        "type": "integer"
                    },
                    "p3": {
                        "type": "object",
                        "properties": {
                            "name": {
                                "type": "string"
                            },
                            "i": {
                                "type": "integer"
                            },
                            "p4": {
                                "type": "object",
                                "properties": {
                                    "name": {
                                        "type": "string"
                                    },
                                    "i": {
                                        "type": "integer"
                                    }
                                }
                            }
                        }
                    }
                }
            }
        },
        "p3": {
            "type": "array",
            "items": {
                "type": "object",
                "properties": {
                    "name": {
                        "type": "string"
                    },
                    "i": {
                        "type": "integer"
                    },
                    "p4": {
                        "type": "object",
                        "properties": {
                            "name": {
                                "type": "string"
                            },
                            "i": {
                                "type": "integer"
                            }
                        }
                    }
                }
            }
        },
        "b": {
            "type": "boolean",
            "required": true
        }
    },
    "classnames": {
        "rootNode": {
            "classname": "com.agent.Person"
        },
        "p2": {
            "classname": "com.agent.Person2",
            "p3": {
                "classname": "com.agent.Person3",
                "p4": {
                    "classname": "com.agent.Person4"
                }
            }
        },
        "p3": {
            "classname": "com.agent.Person3",
            "p4": {
                "classname": "com.agent.Person4"
            }
        }
    }
}

提供给json.loads文件-你知道吗

{
    'classnames': {
        'p2': {
            'classname': 'com.agent.Person2',
            'p3': {
                'classname': 'com.agent.Person3',
                'p4': {
                    'classname': 'com.agent.Person4'
                }
            }
        },
        'p3': {
            'classname': 'com.agent.Person3',
            'p4': {
                'classname': 'com.agent.Person4'
            }
        },
        'rootNode': {
            'classname': 'com.agent.Person'
        }
    },
    'properties': {
        'b': {
            'required': True,
            'type': 'boolean'
        },
        'i': {
            'type': 'integer'
        },
        'name': {
            'type': 'string'
        },
        'p2': {
            'items': {
                'properties': {
                    'i': {
                        'type': 'integer'
                    },
                    'name': {
                        'type': 'string'
                    },
                    'p3': {
                        'properties': {
                            'i': {
                                'type': 'integer'
                            },
                            'name': {
                                'type': 'string'
                            },
                            'p4': {
                                'properties': {
                                    'i': {
                                        'type': 'integer'
                                    },
                                    'name': {
                                        'type': 'string'
                                    }
                                },
                                'type': 'object'
                            }
                        },
                        'type': 'object'
                    }
                },
                'type': 'object'
            },
            'type': 'array'
        },
        'p3': {
            'items': {
                'properties': {
                    'i': {
                        'type': 'integer'
                    },
                    'name': {
                        'type': 'string'
                    },
                    'p4': {
                        'properties': {
                            'i': {
                                'type': 'integer'
                            },
                            'name': {
                                'type': 'string'
                            }
                        },
                        'type': 'object'
                    }
                },
                'type': 'object'
            },
            'type': 'array'
        },
        'strList': {
            'items': {
                'type': 'string'
            },
            'type': 'array'
        },
        'strMap': {
            'type': 'object'
        }
    },
    'type': 'array'
}

作为python的一个序列,他们可以将哪些属性转换成python的另一个序列?你知道吗


Tags: namecomjsonstringobjecttypeitemsinteger
1条回答
网友
1楼 · 发布于 2024-04-20 10:05:22

正如tobiasĩk所说,python字典是无序的,所以一旦将数据加载到其中,就会丢失任何顺序信息。你知道吗

但是,您可以将JSON字符串加载到OrderedDict中:

from collections import OrderedDict
import json

json.loads(your_json_string, object_pairs_hook=OrderedDict)

此方法is mentioned in the ^{} module documentation

相关问题 更多 >