以下の3つのテストケースGet Requests1、Get Requests2およびGet Requests3。私は、3つの異なる方法で/ RestResponse /結果ノードの値を取得しようとしましたが、それは同じエラーが表示さrobotframeworkのRequestsLibraryを使用してjsonデータにアクセスできない:TypeError:期待される文字列またはバッファ
*** Settings ***
Library Collections
Library OperatingSystem
Library HttpLibrary.HTTP
Library RequestsLibrary
*** Test Cases ***
Get Requests1
# create a HTTP session to a server
Create Session countryname http://services.groupkt.com
# store response
${resp}= Get Request countryname /country/get/all
log ${resp.content}
# ${resp.content} display entire json response, now tring to get node value of /RestResponse/result
${getResponseJson} Get Json Value ${resp} /RestResponse/result
# shows error TypeError: expected string or buffer
log ${getResponseJson.content}
Get Requests2
# create a HTTP session to a server
Create Session countryname http://services.groupkt.com
# store response
${resp}= Get Request countryname /country/get/all
log ${resp.content}
# ${resp.content} display entire json response, now tring to get node value of /RestResponse/result
${responseContent}= to json ${resp.content}
# shows error TypeError: expected string or buffer
${getResponseJson} Get Json Value ${responseContent} /RestResponse/result
log ${getResponseJson.content}
Get Requests3
# create a HTTP session to a server
Create Session countryname http://services.groupkt.com
# store response
${resp}= Get Request countryname /country/get/all
log ${resp.content}
# ${resp.content} display entire json response,parse json
${data} Parse Json ${resp}
# shows error TypeError: expected string or buffer
${getResponseJson} Get Json Value ${resp} /RestResponse/result
JSONファイル応答
あなたがいない要求オブジェクトから、JSONからGet Json Value
を呼び出す必要が
{
"RestResponse": {
"messages": [
"Total [249] records found."
],
"result": [
{
"name": "Afghanistan",
"alpha2_code": "AF",
"alpha3_code": "AFG"
},
{
"name": "\ufffd\ufffdland Islands",
"alpha2_code": "AX",
"alpha3_code": "ALA"
},
{
"name": "Albania",
"alpha2_code": "AL",
"alpha3_code": "ALB"
},
{
"name": "Algeria",
"alpha2_code": "DZ",
"alpha3_code": "DZA"
}
]
}
}
返信いただきありがとうございます。 $ {resp.content}で試しました。 $ {getResponseJson.content} 'が失敗しました:AttributeError:' unicode 'オブジェクトに' content '属性がありません。私も$ {resp.json}で試して、TypeError:期待される文字列またはバッファーを表示しました。 – madhur
@madhur:入力したものと入力したものと自分の結果を詳しく見てください。あなたのコメントでは、 '$ {resp.content}'にjson文字列が表示されます。その文字列は、他のデータを検索するために必要なものです。 '$ {resp.json} 'ではなく' $ {getResponseJson.content} 'ではありません。 –
間違いなく、$ {resp.content}にはjson文字列が表示されます。ライブラリメソッドは、この変数にアクセスしようとすると、問題に言及したようにエラーを表示しています。 – madhur