如何处理上传到php服务器的jsonarray字符串?

时间:2012-04-12 14:23:56

标签: php android

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'=&gt; '[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对象它只是打印成字符串的数组

1 个答案:

答案 0 :(得分:1)

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