0

php文件中我正在创建一个2d动态数组,并在返回它时使用编码json_encode

 $ans=array('x'=>array(),'y'=>array(),'z'=>array());
    foreach ($result as &$y)
    { if(strpos($y,"x")){$ans['z'][]=substr(strstr($y,':'),12);}
      else if(strpos($y,"y")){$ans['y'][]=substr(strstr($y,':'),2);}
      else if(strpos($y,"z")){$ans['z'][]=substr(strstr($y,':'),2);} 
     }//$y is a very big string
     return json_encode($ans);

浏览器中的输出显示为{...}IE jsonobject 但是在 java 中我尝试将 httpentity 转换为string(using entityutils.tostring)然后 make

jsonobject ja=new jsonobject(entityutils.tostring(entity));

显示以下
error : org.json.JSONException: A JSONObject text must begin with '{' at 1 [character 2 line 1]

为什么显示这个?

编辑:

List<NameValuePair> namevaluepair=new ArrayList<NameValuePair>(1);
         namevaluepair.add(new BasicNameValuePair("request",""+x)); 
         HttpClient httpclient = new DefaultHttpClient();
         HttpPost httppost = new HttpPost("http://localhost/webc.php");
         httppost.setEntity(new UrlEncodedFormEntity(namevaluepair));
         HttpResponse response = httpclient.execute(httppost);
         HttpEntity entity = response.getEntity();
if(entity!=null)
{String line=EntityUtils.toString(entity);
JSONObject ja=new JSONObject(line);//showing error here
}
4

0 回答 0