简体   繁体   English

如何将浏览器返回的json对象转换为python lambda函数中的字符串

[英]How to convert json object returned from browser to string in python lambda function

Am a beginner to AWS, suppose I received a json object as following from client 是AWS的初学者,假设我从客户端收到了如下的json对象

[{'bid': 1, 'bodypart_name': 'Chest', 'image': 'image'}]

on which service of aws I would receive this object (am assuming it would get received in API gateway) and how should I convert that object to string in my lambda python function. 我将在哪个aws服务上接收该对象(假设它会在API网关中接收),以及如何在lambda python函数中将该对象转换为字符串。 Any help would be much appreciated 任何帮助将非常感激

Ask your client where they are sending this json request. 询问您的客户他们在哪里发送此json请求。 If you are getting this on lambda you'll want to extract the values not turn the whole json object into a string. 如果您在lambda上获取此信息,则需要提取值,而不是将整个json对象转换为字符串。 You can get the values like this: 您可以获取如下所示的值:

json_obj = [{'bid': 1, 'bodypart_name': 'Chest', 'image': 'image'}]
for i in json_obj:
    print(i['bid'])
    print(i['bodypart_name'])
    print(i['image'])

The reason for the for loop is to capture multiple list objects. for循环的原因是捕获多个列表对象。 If you are sure you will only get 1 object inside list you can use json_obj[0]['bid'] without loop. 如果您确定列表中只会得到1个对象,则可以不循环使用json_obj[0]['bid']

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM