Combining two JSON objects in to one

前端 未结 3 538
既然无缘
既然无缘 2020-12-05 19:45

I have two JSON objects. One is python array which is converted using json,dumps() and other contains records from database and is serialized using json serializer. I want t

相关标签:
3条回答
  • 2020-12-05 20:23

    Not sure if I'm missing something, but I think this works (tested in python 2.5) with the output you specify:

    import simplejson
    
    finalObj = { 'obj1': obj1, 'obj2': obj2 }
    simplejson.dumps(finalObj)
    
    0 讨论(0)
  • 2020-12-05 20:31

    You have two techniques. The list version suffers from the limitation that the order matters. However, the JSON is slightly simpler-looking. The dictionary version has nested data, which looks more complex.

    data = { 'obj1' : obj1, 'obj2' : obj2 }
    json.dumps(data,indent=2)
    
    
    data = [ obj1, obj2 ]
    json.dumps(data,indent=2)
    
    0 讨论(0)
  • 2020-12-05 20:48

    You can't do it once they're in JSON format - JSON is just text. You need to combine them in Python first:

    data = { 'obj1' : obj1, 'obj2' : obj2 }
    json.dumps(data)
    
    0 讨论(0)
提交回复
热议问题