如何在java中使用截取解析json

如何在java中使用截取解析json,java,json,android-volley,Java,Json,Android Volley,直到现在,我必须在php中使用数组数据名创建json,如下所示: print "{data : ".json_encode($the_data,JSON_UNESCAPED_UNICODE)."}" String url = "httpmywebsite"; JsonObjectRequest jsonObjectRequest = new JsonObjectRequest(Request.Method.GET, url, ne

直到现在,我必须在php中使用数组数据名创建json,如下所示:

print "{data : ".json_encode($the_data,JSON_UNESCAPED_UNICODE)."}"
String url = "httpmywebsite";
        JsonObjectRequest jsonObjectRequest = new JsonObjectRequest(Request.Method.GET, url,
                        new Response.Listener<JSONObject>() {
                            @Override
                            public void onResponse(JSONObject response) {
                                try {
                                    JSONArray jsonArray = response.getJSONArray("data");
                                    for (int i = 0; i < jsonArray.length(); i  ) {
                                        JSONObject respons = jsonArray.getJSONObject(i);
                                        String id = respons.getString("id");
                                        String name = respons.getString("name");
                                        String email = respons.getString("email");

}
                                } catch (JSONException e) {
                                    e.printStackTrace();
                                }
                            }
                        }, new Response.ErrorListener() {
                    @Override
                    public void onErrorResponse(VolleyError error) {
                        Log.e("VOLLEY", "ERROR");
                    }
                }

                );
                requestQueue.add(jsonObjectRequest);
            }
在java的android studio中,我喜欢这样做:

print "{data : ".json_encode($the_data,JSON_UNESCAPED_UNICODE)."}"
String url = "httpmywebsite";
        JsonObjectRequest jsonObjectRequest = new JsonObjectRequest(Request.Method.GET, url,
                        new Response.Listener<JSONObject>() {
                            @Override
                            public void onResponse(JSONObject response) {
                                try {
                                    JSONArray jsonArray = response.getJSONArray("data");
                                    for (int i = 0; i < jsonArray.length(); i  ) {
                                        JSONObject respons = jsonArray.getJSONObject(i);
                                        String id = respons.getString("id");
                                        String name = respons.getString("name");
                                        String email = respons.getString("email");

}
                                } catch (JSONException e) {
                                    e.printStackTrace();
                                }
                            }
                        }, new Response.ErrorListener() {
                    @Override
                    public void onErrorResponse(VolleyError error) {
                        Log.e("VOLLEY", "ERROR");
                    }
                }

                );
                requestQueue.add(jsonObjectRequest);
            }

所以我试着更新我的java代码来进行类似下面的截击,但没有成功

JSONArray jsonArray= new JSONArray(response.toString());
新的php代码返回以下json:

[{"0":"100","id":"100","1":"sandra","name":"sandra","2":"x@x.com","email":"x@x.com"}]
我添加了这段代码来获取截击的错误类型

@Override
公共无效onErrorResponse(截击错误){


我收到这样一条消息:ParseError

这是php的精确输出吗

[{“0”:“100”,“id”:“100”,“1”:“桑德拉”,“姓名”:“桑德拉”,“2”:”x@x.com,“电子邮件”:x@x.com“}]

因为如果是这种情况,您将得到一个错误,因为没有json数组“数据”
这是它的附件。

请包括所有新代码,而不仅仅是一行。其余的保持不变,我刚刚替换了这一行:
JSONArray JSONArray=jsonObject.getJSONArray(“数据”);
这一行:
JSONArray JSONArray=new JSONArray(response.toString())
Hmm我看不出它不起作用的原因。你说的“不起作用”是什么意思?你测试过你的php页面返回正确的数据了吗?它在代码中的确切位置崩溃了吗?消息是什么?请更新问题。是的,我检查过php json结果。我得到这个结果:
[{“0”:“100”,“id”:“100”,“1”:“桑德拉”,“姓名”:“桑德拉”,“2”:x@x.com,“电子邮件”:x@x.com“}]
它崩溃了吗?你说“不工作”是什么意思?我想,你没有正确地阅读我的问题。我想删除“数据”“从我的php代码中,但是截取之后应该可以工作,我得到以下消息:ParseError。截取必须得到数据名吗?当截取得到json作为数组时,这还不够吗?”?
if (error instanceof TimeoutError || error instanceof NoConnectionError) {
    Toast.makeText(context,
            context.getString(R.string.error_network_timeout),
            Toast.LENGTH_LONG).show();
} else if (error instanceof AuthFailureError) {
    //TODO
} else if (error instanceof ServerError) {
   //TODO
} else if (error instanceof NetworkError) {
  //TODO
} else if (error instanceof ParseError) {
Toast.makeText(context,
            "ParseError",
            Toast.LENGTH_LONG).show();
    }
    }