【发布时间】:2018-03-05 03:41:19
【问题描述】:
非常简单的 json 被调用和解析,但无论我尝试什么,我都会得到以下结果
com.android.volley.ParseError: org.json.JSONException: Value $items of 类型 java.lang.String 无法转换为 JSONObject
以下 json 是在 PHP 脚本中创建的。当通过 Volley 请求调用时,脚本会加载 json,操作几个字段,然后将其保存回文件。
json
$items=array (
0 =>
array (
'first' => 'Tom',
'last' => 'Sawyer',
'email' => 'u@gmail.com',
'phone' => '1112223344',
'zip' => '11122',
'status' => '3',
),
);
$unObiect->items = $items;
$json = json_encode($unObiect);
echo($json);
json 是通过
创建和保存的$data[]=array(
'first'=>$temp[0]['first'],
'last'=>$temp[0]['last'],
'phone'=>$temp[0]['phone'],
'email'=>$temp[0]['email'],
'zip'=>$temp[0]['zip'],
);
file_put_contents($filename, '$items=', FILE_APPEND);
file_put_contents($filename, var_export($data,true) . ';'."\n", FILE_APPEND);
file_put_contents($filename, '$unObiect->items = $items;'."\n", FILE_APPEND);
file_put_contents($filename, '$json = json_encode($unObiect);'."\n", FILE_APPEND);
file_put_contents($filename, 'echo($json);'."\n", FILE_APPEND);
file_put_contents($filename, '?>', FILE_APPEND);
截击请求
JsonObjectRequest jsonObjectRequest = new JsonObjectRequest(Request.Method.GET, dataurl, new Response.Listener <JSONObject>() {
@Override
public void onResponse(JSONObject response) {
if (response != null) {
try {
userArray = response.getJSONArray("items");
for (int i = 0; i < userArray.length(); i++) {
JSONObject tempObj = userArray.getJSONObject(i);
UserData userData = new UserData();
userData.setFirst_name(tempObj.getString("first"));
userData.setLast_name(tempObj.getString("last"));
userData.setUser_phone(tempObj.getString("phone"));
userData.setEmail(tempObj.getString("email"));
userData.setTrialdays(tempObj.getString("day"));
userData.setStatus(tempObj.getString("status"));
userList.add(userData);
}
} catch (JSONException e) {
e.printStackTrace();
}
}
}
}, new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
Toast.makeText(SplashActivity.this, "No Registration. . ." + error, Toast.LENGTH_SHORT).show();
checkBox();
}
});
我觉得这与修改后 json 保存回来的方式有关,但我无法指出它主要是因为我正在使用第二个 json 文件,该文件具有在一秒钟内以相同方式创建的不同信息凌空请求并没有收到错误,加载完美。任何建议将不胜感激。
【问题讨论】:
-
发布您的 json 响应..
标签: php android json android-volley