0

我想在 Google App Engine 上读取 JSON 字符串(从我的 Android 应用程序获取)。但我不知道如何解决这个问题。

这些是我的应用程序中的代码:

    JSONObject ob = new JSONObject();
    ob.put("user", "peter");
    ob.put("frage", "frage1");
    ob.put("datumende", "27.4.2012");
    Log.i("json", ob.toString());

    connection.setRequestMethod("POST");
    connection.setRequestProperty("Content-Type",
            "application/x-www-form-urlencoded");

    connection.setRequestProperty("Content-Language", "en-US");

    connection.setUseCaches(false);
    connection.setDoInput(true);
    connection.setDoOutput(true);

    byte[] outputBytes = ob.toString().getBytes("UTF-8");
    connection.setRequestProperty("Content-Length", Integer.toString(outputBytes.length));
    OutputStream os = connection.getOutputStream();
    os.write(outputBytes);

有用。我可以从我的应用引擎中读取这些数据

getPostData = self.request.body

如果我将这些(getPostData)写在它输出的数据存储中

{\"datumende\":\"27.4.2012\",\"user\":\"peter\",\"frage\":\"frage1\"}

它也有效。

但是如何将 JSON-String 拆分为 user、datumende 和 fage?我已经测试了它

x = json.dumps(self.request.body)
y = json.loads(x)
# I thougt in y['user'] is now "peter". But it isnt't

我也只测试

x = json.laods(self.request.body)
# I thougt in x['user'] is now "peter". But it isnt't

但都不起作用。那么,如何将 JSON-String 拆分为 user、frage、datumende?

4

1 回答 1

0

简单的回答:/

客户端站点(Android):替换

connection.setRequestProperty("Content-Type",
        "application/x-www-form-urlencoded");

connection.setRequestProperty("Content-Type", "application/json");

这些在服务器站点上(使用 Python 的 GAE):

lo = self.request.body
fff = json.loads(lo)
name = fff['user']
于 2013-04-27T14:01:51.360 回答