将多个Json对象组合成一个对象?

时间:2018-08-23 21:01:45

标签: python json dictionary

我坚持创建单个json对象,希望有人能提供帮助。

def loadit():
r = sess.get(url)
data = r.json()
z = data['styles']
for zz in z:
    ID = zz['id']
    color = zz['name']
    sizes = zz['sizes']
    for s in sizes:
        stock = s['stock_level']
        size = s['name']
        ids = s['id']
        logit(stock,size,ids,ID,color)

def logit(stock,size,ids,ID,color):
    stock = stock
    size = size
    ids = ids
    ID = ID
    color = color
    data = {ID: []}
    arr = [{'Color':color,'Size':size,'Stock':stock}]
    data[ID].append(arr)
    o = json.dumps(data)

我希望将这些循环对象转换为1个易于按ID解析的json

   {"21239": [[{"Color": "Pale Yellow", "Stock": 0, "Size": "Large"}]]}
   {"21239": [[{"Color": "Pale Yellow", "Stock": 0, "Size": "XLarge"}]]}
   {"21240": [[{"Color": "Red", "Stock": 0, "Size": "Small"}]]}
   {"21240": [[{"Color": "Red", "Stock": 0, "Size": "Medium"}]]}
   {"21240": [[{"Color": "Red", "Stock": 0, "Size": "Large"}]]}
   {"21240": [[{"Color": "Red", "Stock": 0, "Size": "XLarge"}]]}
   {"21241": [[{"Color": "Terra Cotta", "Stock": 0, "Size": "Small"}]]}
   {"21241": [[{"Color": "Terra Cotta", "Stock": 0, "Size": "Medium"}]]}

0 个答案:

没有答案