j化数组

2024-09-29 19:09:25 发布

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

我有以下内容:

def jsonify(ar):
    json.dumps(ar._data)

jsonify(getFromTable())

getFromTable返回一个boto对象数组。每个对象都有一个数据元素。但是ar.\u数据不起作用。它没有属性\u data。在

如何从多个对象生成一个单独的json。还是不可能?在

我的工作是:

^{pr2}$

不过,我还是会优先在一个json blob中打印它们。有人知道怎么做吗?在

在mGilson的帮助下解决了以下问题

同时作为参考:

我使用的是boto、dynamodb2、python,并从查询表返回的lazy evaluation resultSet中提取。在


Tags: 数据对象json元素data属性def数组
1条回答
网友
1楼 · 发布于 2024-09-29 19:09:25

@mGilson,谢谢。这是解决这个问题的正确方法。在

对于任何好奇的人来说,这里是我使用的实现。在

def getFromTable():
    global table
    #t = table.scan(thirdKey__eq="Anon")
    t = table.scan()
    arr = []
    for a in t:
        arr.append(a)
    return arr

def jsonify(ar):
    str = json.dumps(ar)
    print str
    return str

def createListFromBotoObj(obj):
    myList = []
    for o in obj:
        myList.append(o._data)
    return myList
jsonify(createListFromBotoObj(getFromTable()))

打印出预期结果:

[{"secondKey": "Doe", "thirdKey": "Anon", "firstKey": "John"}, {"secondKey": "G", "thirdKey": "Company", "firstKey": "P"}, {"secondKey": "T", "thirdKey": "Engineer3", "firstKey": "allen"}, {"secondKey": "John", "last_name": "Doe", "firstKey": "booperface"}, {"secondKey": "The Builder", "thirdKey": "Sadness", "firstKey": "Bob"}]

如果有人想知道我用这个来测试我将如何实现我的实际数据库。在

相关问题 更多 >

    热门问题