0

c我正在使用此代码将 jsonarray 上传到服务器(也许有更好的方法):

            ArrayList<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>();
            nameValuePairs.add(new BasicNameValuePair("json_a", json_a.toString()));

            httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));
            HttpResponse response = httpclient.execute(httppost);
            String responseText = EntityUtils.toString(response.getEntity());

我在 LogCat 中得到的响应是:

04-10 23:05:39.833: D/ProviderTester(601): The response text is POST = array (
04-10 23:05:39.833: D/ProviderTester(601):   'json_a' => '[name=Mike, datetime=2012-04-10 21:29]\\",\\"[name=Roger, datetime=2012-03-10 21:29]\\"]\\"]',
04-10 23:05:39.833: D/ProviderTester(601): )
04-10 23:05:39.833: D/ProviderTester(601): GET = array (
04-10 23:05:39.833: D/ProviderTester(601): )
04-10 23:05:39.833: D/ProviderTester(601): request = array (
04-10 23:05:39.833: D/ProviderTester(601):   'Content-Length' => '174',
04-10 23:05:39.833: D/ProviderTester(601):   'Content-Type' => 'application/x-www-form-urlencoded',
04-10 23:05:39.833: D/ProviderTester(601):   'Host' => 'graasdfon.hostei.com',
04-10 23:05:39.833: D/ProviderTester(601):   'Connection' => 'Keep-Alive',
04-10 23:05:39.833: D/ProviderTester(601):   'User-Agent' => 'Apache-HttpClient/UNAVAILABLE (java 1.4)',
04-10 23:05:39.833: D/ProviderTester(601):   'Expect' => '100-Continue',
04-10 23:05:39.833: D/ProviderTester(601): )
04-10 23:05:39.833: D/ProviderTester(601): 
04-10 23:05:39.833: D/ProviderTester(601): <!-- www.000webhost.com Analytics Code -->

php 脚本服务器端如下所示:

<?php

$out="POST = " . var_export($_POST, true) . "\n";
$out.="GET = " . var_export($_GET, true) . "\n";
$out.="request = " . var_export(getallheaders(), true) . "\n";
print $out;

?>

我看到 'json_a' => '[name=Mike, datetime=2012-04-10 21:29]\",\"[name=Roger, datetime=2012-03-10 21:29]\"]\ "]' 包含我想要的数据,但是如何从对象中取出名称和日期?

编辑:当我在 logcat 中打印出 JSON 对象时,这就是它的样子(不是来自服务器的响应):

 4-10 23:33:36.013: I/JSONInfo(601): JSON object: ["[name=mike, datetime=2012-04-10 23:33]","[name=red, datetime=2012-04-10 23:12]"]

我不确定 ["[name=mike, datetime=2012-04-10 23:33]","[name=red, datetime=2012-04-10 23:12]"] 是否是正确的 json 对象打印成字符串的数组

4

1 回答 1

1

如果 JSON 正确,您可以使用json_decode()将数据作为数组或对象获取。实际上,您发送的内容看起来不是有效的 JSON。

于 2012-04-12T14:26:21.867 回答