我正在尝试从python发送字典:
def toJsonCustom(self):
ajson = []
ajson.append({
'id': self.id,
'data': self.data
})
return json.dumps(
ajson, default=lambda obj: obj.__dict__, separators=(',', ':'),indent=4,
)
async def curl(url,data):
async with aiohttp.ClientSession() as session:
async with session.post(url, json={'datajson':data}) as response:
print(repr(response))
chunk = await response.content.read()
print('Downloaded: %s' % len(chunk))
#output JSON:
"{"datajson": "[\n {\n \"id\":\"test\",\n \"data\":\"test\"\n }\n]"}"
当尝试将此 json 的输出转换为 javascript 的对象时,我不确定。
const data = JSON.parse(get(props, 'websocket.data', null)) || get(state, 'websocket.data') || 'No data';
console.log('Object json: ', (data));
# output: {"datajson": "[ n { n "id ": "test " n "data ": "test " n } n]"}
console.log('Object json: ', (data.id));
# output:undefined
如何以正确的方式将 json 转换为 javascript 对象?
谢谢
答案 0 :(得分:1)
我认为您需要data.datajson[0].id
答案 1 :(得分:0)
似乎您需要两次解析JSON字符串才能获得所需的对象:
{ "datajson": "[{..." }
,请注意,datajson
属性的值是一个字符串 nestedData = JSON.parse(data.datajson)