【问题标题】:JSONArray from android to Array phpJSONArray 从 android 到 Array php
【发布时间】:2013-09-12 10:57:39
【问题描述】:

我正在将一些值从 android/java 发送到服务器。

首先我创建一个 JSONArray 对象:

JSONArray JSONsamples = new JSONArray();

然后我按以下方式填充 JSONArray:

 for(.....)
                {
                    if(...)
                    {
                      JSONsamples.put(storedArraylist.get(i));
                    }
                }

然后我通过 post 发送值:

userParameters.add(new BasicNameValuePair("samples", JSONsamples.toString()));
.
.
Http_Client.executeHttpPost(getApplicationContext(), POST_url, userParameters);
.

然后在服务器上我尝试将这些值存储到 php 数组中,但它不起作用:

 $samplesarray = $_POST["samples"];
 $samplesarray = json_decode($samplesarray,true);

在解码之前,如果我回显 $samplesarray ,我只会得到封装在这些 \"value\" 中的第一个值,在 json_decode 之后,当我回显 $samplesarray 时,我会得到一个预期的参数但发现 null 错误。

请帮忙。

【问题讨论】:

  • echo $samplesarray 看看你得到了什么并告诉我们
  • 看来\"blood\"JSONsamples.toString() 返回的内容。或者如果您启用了magic_quotes,它可能是"blood"JSONsamples.toString() 的预期返回值是多少?
  • 你的 Android 代码中 JSONSamples.toString() 的输出是什么
  • \"blood\" 不是有效的 JSON,这就是我认为的问题。
  • 忽略生成的[]!发生这种情况是因为您不能json_decode\"blood\" 这样的字符串。 JSONsamples.toString() 应该返回如下内容:["value1","value2","value3"]{"key1":"value1","key2":"value2"},您需要禁用 magic_quotes!

标签: php android json


【解决方案1】:

Magic Quotes 应该关闭,因为它会在您的 " 值之前添加一个 \。 这会破坏您案例中的 json 数据。

请在此处详细了解为什么不应该使用魔术引号: http://php.net/manual/en/security.magicquotes.whynot.php

【讨论】:

    【解决方案2】:

    解决方案是删除魔术引号,或者至少检查它是否打开并从那里开始工作。 @steven 让我想起了这一点,谢谢。

    所以在服务器端,我做了以下检查以确保正确的 json 并且它可以工作:

    $samplesarray = get_magic_quotes_gpc() ? stripslashes($_REQUEST['samples']) : $_REQUEST['samples'];
    

    【讨论】:

      猜你喜欢
      • 2018-03-25
      • 1970-01-01
      • 1970-01-01
      • 2019-05-12
      • 2011-11-22
      • 2017-12-14
      • 1970-01-01
      • 2017-03-05
      相关资源
      最近更新 更多