フォームを作成してサーバーに保存する Google Forms クローンを作成しています。フォーム ビルダーは、次の JSON.stringify 形式を出力します。
{"method":"post","action":"/test","html":[{"input_type":"input_text","caption":"What is your name?"},{"input_type":"radio","caption":"What is the name of your dog?","options":{"benny":"Benny","billy":"Billy","bobby":"Bobby"}}]}
これを次のように App Engine バックエンドに送信しようとしています。
$.ajax({
type: "POST",
url: save_url,
contentType: "application/json; charset=utf-8",
dataType: "json",
data: json_string,
success: function (e) {
console.log(e);
}
});
しかし、サーバー上でこの json 文字列を「開いて」、データベースに挿入するにはどうすればよいでしょうか?
最初の質問:self.request.body
データ オブジェクト (json 文字列) を取得するために使用する必要がありますか、またはそれを取得するためのより良い方法があります。現在、適切な形式を取得するために文字列をデコードする必要があります。
def post(self):
form_elements = json.loads(urllib.unquote_plus(self.request.body))
self.write(form_elements)
2 番目の質問: json.loads
json 文字列を解析するために使用すると、次のエラーが表示ValueError: No JSON object could be decoded
されます。