将对象列表序列化为json数组列表

时间:2017-04-27 08:46:50

标签: python json

我的对象是这样实现的:

class TestLog:
    def __init__(self):
        self.test_name = "Unknown"
        self.test_package = "Unknown"
        self.test_status = "Unknown"
        self.device = "Unknown"
        self.test_start_time = -1
        self.test_end_time = -1
        self.ran_times = 1
        self.error_messages = list()

    def __dict__(self):
        test_log_dict = dict()
        test_log_dict["test_name"] = self.test_name
        test_log_dict["test_package"] = self.test_package
        test_log_dict["test_status"] = self.test_status
        test_log_dict["device"] = self.device
        test_log_dict["test_start_time"] = self.test_start_time
        test_log_dict["test_end_time"] = self.test_end_time
        test_log_dict["ran_times"] = self.ran_times
        test_log_dict["error_messages"] = self.error_messages
        return test_log_dict

我可以把它变成JSON:

import json

log1 = TestLog()
log2 = TestLog()
json_dump1 = json.dumps(log1.__dict__())
json_dump2 = json.dumps(log2.__dict__())

输出是 - 2个独立的json对象:

LOG1:

{
    "test_name": "Unknown",
    "test_package": "Unknown",
    "test_status": "Unknown",
    "device": "Unknown",
    "test_start_time": -1,
    "test_end_time": -1,
    "ran_times": 1,
    "error_messages": []
}

的log 2:

{
    "test_name": "Unknown",
    "test_package": "Unknown",
    "test_status": "Unknown",
    "device": "Unknown",
    "test_start_time": -1,
    "test_end_time": -1,
    "ran_times": 1,
    "error_messages": []
}

我想知道这个结果:

{
    "test_results": [
        {
            "test_name": "Unknown",
            "test_package": "Unknown",
            "test_status": "Unknown",
            "device": "Unknown",
            "test_start_time": -1,
            "test_end_time": -1,
            "ran_times": 1,
            "error_messages": []
        },
        {
            "test_name": "Unknown",
            "test_package": "Unknown",
            "test_status": "Unknown",
            "device": "Unknown",
            "test_start_time": -1,
            "test_end_time": -1,
            "ran_times": 1,
            "error_messages": []
        }
    ]
}

但这种方法对我不起作用:

    log_dict = dict()
    log_dict.update({"test_results": [json_dump1, json_dump2]})
    list_dump = json.dumps(log_dict)

因为这种情况发生了:

{" test_results":[" {\" test_name \":\" Unknown \",\" test_package \ ":\"未知\",\" test_status \":\"未知\",\"设备\&# 34;:\" Unknown \",\" test_start_time \":-1,\" test_end_time \":-1,\" ran_times \":1,\" error_messages \":[]}"," {\" test_name \":\&#34 ;未知\",\" test_package \":\"未知\",\" test_status \":\"未知\",\"设备\":\"未知\",\" test_start_time \":-1,\" test_end_time \":-1,\" ran_times \":1,\" error_messages \":[]}"]}

2 个答案:

答案 0 :(得分:0)

json.dumps返回一个字符串,因此您实际上是将字符串附加到log_dict['test_results']

相反,将整个字典作为一个整体创建,然后一次性将其转换为json字符串:

log_dict = dict()
log_dict.update({"test_results": [log1.__dict__(), log2.__dict__()]})
list_dump = json.dumps(log_dict)
print(list_dump)

# {
#       "test_results": [{
#       "device": "Unknown",
#       "error_messages": [],
#       "test_status": "Unknown",
#       "test_start_time": -1,
#       "ran_times": 1,
#       "test_package": "Unknown",
#       "test_name": "Unknown",
#       "test_end_time": -1
#   }, {
#       "device": "Unknown",
#       "error_messages": [],
#       "test_status": "Unknown",
#       "test_start_time": -1,
#       "ran_times": 1,
#       "test_package": "Unknown",
#       "test_name": "Unknown",
#       "test_end_time": -1
#   }]
# }

顺便说一下,如果你想要实现的话,你甚至不需要实现__dict__。默认的__dict__方法已经为您完成,可以通过调用对象上的vars来调用:

import json

class TestLog:
    def __init__(self):
        self.test_name = "Unknown"
        self.test_package = "Unknown"
        self.test_status = "Unknown"
        self.device = "Unknown"
        self.test_start_time = -1
        self.test_end_time = -1
        self.ran_times = 1
        self.error_messages = list()


log1 = TestLog()
log2 = TestLog()


log_dict = dict()
log_dict.update({"test_results": [vars(log1), vars(log2)]})
list_dump = json.dumps(log_dict)

print(list_dump)

# {
#       "test_results": [{
#       "device": "Unknown",
#       "error_messages": [],
#       "test_status": "Unknown",
#       "test_start_time": -1,
#       "ran_times": 1,
#       "test_package": "Unknown",
#       "test_name": "Unknown",
#       "test_end_time": -1
#   }, {
#       "device": "Unknown",
#       "error_messages": [],
#       "test_status": "Unknown",
#       "test_start_time": -1,
#       "ran_times": 1,
#       "test_package": "Unknown",
#       "test_name": "Unknown",
#       "test_end_time": -1
#   }]
# }

使用更简单的类定义实现了相同的结果。

答案 1 :(得分:0)

当然,因为你需要一个字典作为值,但是你要存储代表序列化字典的json字符串。

我会这样做:

log_dict = {"test_results": [log1.__dict__(), log2.__dict__()]})

如果之前必须冻结log1log2,请复制该词组

log1_dict = log1.__dict__().copy()
log2_dict = log2.__dict__().copy()

然后:

log_dict = {"test_results": [log1_dict, log2_dict]})