【问题标题】:why json array retrieve just first record为什么json数组只检索第一条记录
【发布时间】:2016-07-27 16:00:09
【问题描述】:

这个问题是关于从 mysql 解析 json 的。我想在我的 android 应用程序中创建一个登录页面。 这是我的问题: 当应用程序处于调试状态时(实际上是在运行时!),response(String json) 的值包含所有元素,然后将它们引用到我的 ParseJSONuser。在这个活动中,jsonObject 的值也包含 json 中的所有元素,但是在我的 jsonArray 中,我看到了 json 的第一条记录,当然还有 jsonArray.length() = 1,为什么 jsonArray 只检索第一个元素(或一条json记录)。

这是 ParseJSON 类:

public class ParseJSONUser {

private static String name_get, pass_get, email_get, imageid_get, credit_get, state_get;

public static final String JSON_ARRAY = "user";
public static final String KEY_NAME = "name";
public static final String KEY_EMAIL = "email";
public static final String KEY_PASS = "pass";
public static final String KEY_IMAGEID = "imageid";
public static final String KEY_CREDIT = "credit";
public static final String KEY_STATE = "state";

public ArrayList<ArrayList<String>> repository;

private JSONArray jsonArray = null;

private String json;

public ParseJSONUser(String json){
    this.json = json;
}

protected void parseJSONuser(){

    repository = new ArrayList<ArrayList<String>>();

    JSONObject jsonObject = null;
    try {

        jsonObject = new JSONObject(json);
        jsonArray = jsonObject.getJSONArray(JSON_ARRAY);

        for(int i = 0; i<jsonArray.length() ; i++){
            JSONObject jo = jsonArray.getJSONObject(0);
            name_get = jo.getString(KEY_NAME);
            email_get = jo.getString(KEY_EMAIL);
            pass_get = jo.getString(KEY_PASS);
            imageid_get = jo.getString(KEY_IMAGEID);
            credit_get = jo.getString(KEY_CREDIT);
            state_get = jo.getString(KEY_STATE);

            ArrayList<String> al = new ArrayList<>();
            al.add(0,name_get);
            al.add(1,email_get);
            al.add(2, pass_get);
            al.add(3, imageid_get);
            al.add(4, credit_get);
            al.add(5, state_get);

            repository.add(al);
        }
    } catch (JSONException e) {
        e.printStackTrace();
    }
}

而且,我在我的活动中使用这种方法从主机获取数据(很清楚!)

    private void sendRequest() {

    StringRequest stringRequest = new StringRequest(URL_GET,
            new Response.Listener<String>() {
                @Override
                public void onResponse(String response) {
                    checkJSONuser(response);
                }
            },
            new Response.ErrorListener() {
                @Override
                public void onErrorResponse(VolleyError error) {
                    Toast.makeText(GateActivity.this, error.getMessage(), Toast.LENGTH_LONG).show();
                }
            });

    RequestQueue requestQueue = Volley.newRequestQueue(this);
    requestQueue.add(stringRequest);
}

而且,我的主机中有 php 文件,用于获取所有表格(效果很好) 这是我的 php 代码:https://gist.github.com/sadeghmohebbi/ef6e1e7b5a529596396a7d58f462ef7e

对不起我的英语。请帮忙...谢谢

【问题讨论】:

  • 又不行了

标签: php android mysql json jsonobject


【解决方案1】:

那是因为您总是在循环中获得第一个位置。
您应该使用i 参数:

...
for(int i = 0; i<jsonArray.length() ; i++){
    JSONObject jo = jsonArray.getJSONObject(i);
...

【讨论】:

  • 没有区别...在我的 jsonArray 中只有一条记录。谢谢,但这对我不起作用...我在调试中检查它
  • 请发布您的 Json,可能您使用了错误的标签
  • 这是我的 php 文件和结果(json):gist.github.com/sadeghmohebbi/ef6e1e7b5a529596396a7d58f462ef7e
  • 正如我在示例中看到的,根不是数组。你应该有users[{"user"}..{"user"}]
  • 我该怎么办?我没有学好php。请帮我处理这个编码
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多