GAE从Android应用程序读取JSON字符串

2024-10-04 05:26:02 发布

您现在位置:Python中文网/ 问答频道 /正文

我想在googleappengine上读取一个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);

它起作用了。我可以从我的应用程序引擎读取这些数据

^{pr2}$

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

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

它也起作用了。在

但是如何将JSON字符串拆分为user、datumende和frage呢? 我已经测试过了

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字符串拆分为user、frage、datumende?在


Tags: 字符串json应用程序putcontentconnectionpeteruser
1条回答
网友
1楼 · 发布于 2024-10-04 05:26:02

简单答案:/

客户端站点(Android): 更换

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

^{pr2}$

服务器站点上的这些(GAE和Python):

lo = self.request.body
fff = json.loads(lo)
name = fff['user']

相关问题 更多 >