我的程序有一个JSON类,我需要访问其中的某些项目。
我并不完全了解它是什么类型的数据结构,这可能是我的问题的一部分。
我已尝试json.dumps()
和json.load()
,两者都会返回错误。我甚至试过._dict_
。
我收到以下错误:
"the JSON object must be str, bytes or bytearray, not 'LambdaContext'," "'LambdaContext' object has no attribute '_dict_'," and "Object of type 'LambdaContext' is not JSON serializable." I don't know what else to do with this JSON data.
我需要访问" apiAccessToken。"
JSON数据:
{
"context": {
"System": {
"apiAccessToken": "AxThk...",
"apiEndpoint": "https://api.amazonalexa.com",
"device": {
"deviceId": "string-identifying-the-device",
"supportedInterfaces": {}
},
"application": {
"applicationId": "string"
},
"user": {}
}
}
}
我的代码:
def postalCodeRetriever(intent, session, context):
deviceId = session['user']['userId']
jsoninfo = json.dumps(context)
json_dict = json.loads(jsoninfo)
print(str(json_dict))
TOKEN = context["System"]
print(TOKEN)
URL = "https://api.amazonalexa.com/v1/devices/" + deviceId + "/settings/address/countryAndPostalCode"
HEADER = {'Accept': 'application/json', 'Authorization': 'Bearer ' + TOKEN}
response = urllib2.urlopen(URL, headers=HEADER)
data = json.load(response)
postalCode = data['postalCode']
return build_response({}, build_speechlet_response(
"hello", postalCode, None, True))
答案 0 :(得分:2)
下面的代码应该这样做:
import json
data = json.dumps({
"context": {
"System": {
"apiAccessToken": "AxThk...",
"apiEndpoint": "https://api.amazonalexa.com",
"device": {
"deviceId": "string-identifying-the-device",
"supportedInterfaces": {}
},
"application": {
"applicationId": "string"
},
"user": {}
}
}
})
data_dict = json.loads(data)
print(data_dict['context']['System']['apiAccessToken'])
输出:
AxThk...