2017-01-20 47 views
0
def lambda_handler(event, context): 
    "idsA=event['items'][0]['ids']" 

當我想與Python調用包含陣列ID的JSON對象的項目。在AWS的λ,從API, 它返回此錯誤:字符串的索引必須是整數AWS API拉姆達

"string indices must be integers"` 

我覺得這個問題可以在配置測試賽:

{ 
    "ids": "ids", 
    "contents": "contents", 
    "ResultID": "ResultID", 
    "QueryID": "QueryID", 
    "nR": "nR", 
    "items": "ids"  


} 

或在JSON體映射模板API getaaway:

{ 
    "items": [ 
    #foreach($elem in $input.params('items').split(',')) 
     { 
      "ids": "$elem.ids", 
      "contents": "$elem.contents" 
     }#if($foreach.hasNext),#end 
    #end 
    ], 
    "QueryID": "$input.params('QueryID')", 
    "nR": "$input.params('nR')" 
} 
+0

假設你的文章中的第一個詞典被稱爲'event',那麼'event ['items'] [0]'返回鍵'items'的值的第一個字母,這是你試圖切片的一個字符串使用另一個'string'。如果我誤解了,請發佈'event'看起來像什麼。 –

+0

檢查我的更新 – user2165656

+0

對不起,仍然沒有幫助我理解。正如我之前提到的那樣,問題在於你正在用另一個字符串索引一個字符串。它必須意味着'event ['items'] [0]'是一個字符串,而不是你所假設的字典。 –

回答

1

這聽起來像在您的測試活動的項目應該是對象的JSON數組:

{ 
    "ResultID": "ResultID", 
    "QueryID": "QueryID", 
    "nR": "nR", 
    "items": [ 
     { 
      "ids": [1, 2, 3], 
       "contents": ["content1", "content2"] 
     } 
    ] 
} 

...但我認爲有可能是一個模板映射的問題在這裏。對API網關的原始請求是什麼樣的?

+0

謝謝!它似乎有效,但1d int數組的輸出格式很奇怪: – user2165656

+0

[「1」,「,」,「2」,「,」,「3」] – user2165656

+0

ids在這個JSON中只是一個字符串,重新訪問它作爲數組。你想讓它成爲JSON中的數組嗎? –

相關問題