python 手动拼接json数组

 #!/usr/bin/env python
 # -*- coding: utf-8 -*-

import time
import json
import random

json_key = ["sensor1", "sensor2"]
json_value = []

while True:
    json_value.clear()
    ra = random.randint(0,9)
    print(ra)
    if ra % 2 == 1:
        json_value.append(1)
    else:
        json_value.append(0)
    if ra % 2 == 1:
        json_value.append(0)
    else:
        json_value.append(1)

    json_dic = dict(zip(json_key, json_value))
    j = json.dumps(json_dic)
    print(j)
    time.sleep(1)

 

你可能感兴趣的:(Python)