如何操作JSONArray从PHP到Android

时间:2017-10-13 07:17:04

标签: php android json android-volley

所以我有这个返回JSONArray的PHP文件。

[
  {
    "name": "SAMPLE NAME 1",
    "number": "12345",
    "entity": "User"
  },
  {
    "name": "SAMPLE NAME 2",
    "number": "67890",
    "entity": "Admin"
  }
]

我正在使用android volley为我的Android应用程序。我想展示一个将分别显示这些名字的Toast。

这是我的android代码:

StringRequest stringReq = new StringRequest(Request.Method.POST, "http://myfile.php", new Response.Listener<String>() 
{
        @Override
        public void onResponse(String response) {
            try {
                JSONObject json = new JSONObject(response);
                Toast.makeText(getApplicationContext(), response, Toast.LENGTH_LONG).show();
            } catch (JSONException e) {
                e.printStackTrace();
            }
        }
    }

4 个答案:

答案 0 :(得分:0)

您的回复位于JSONArray,并且您尝试在JSONObject中解析

尝试解析你的JSON

 try {

        JSONArray jsonArray = new JSONArray("response");
        for (int i = 0; i < jsonArray.length(); i++) {
            JSONObject object = jsonArray.getJSONObject(i);
            String name = object.getString("name");
            String number = object.getString("number");
            String entity = object.getString("entity");

        }

    } catch (JSONException e) {
        e.printStackTrace();
    }

答案 1 :(得分:0)

尝试使用JsonArrayRequest这个

        JsonArrayRequest req = new JsonArrayRequest("your url goes here",
        new Response.Listener<JSONArray>() {
            @Override
            public void onResponse(JSONArray response) {
                Log.d(TAG, response.toString());

                try {

                    jsonResponse = "";
                    for (int i = 0; i < response.length(); i++) {

                        JSONObject jObject = (JSONObject) response
                                .get(i);

                    }

                } catch (JSONException e) {
                    e.printStackTrace();
                    Toast.makeText(getApplicationContext(),
                            "Error: " + e.getMessage(),
                            Toast.LENGTH_LONG).show();
                }
             }
        }, new Response.ErrorListener() {
            @Override
            public void onErrorResponse(VolleyError error) {
                VolleyLog.d(TAG, "Error: " + error.getMessage());
                Toast.makeText(getApplicationContext(),
                        error.getMessage(), Toast.LENGTH_SHORT).show();
            }
        });

答案 2 :(得分:0)

 try {

    JSONArray j = new JSONArray("response");
    for (int i = 0; i < j.length(); i++) {
        JSONObject object = j.getJSONObject(i);
        String name = object.getString("name");
        String number = object.getString("number");
        String entity = object.getString("entity");

    }

} catch (JSONException e) {
    e.printStackTrace();
}

答案 3 :(得分:0)

您的回复数据是一个数组。您需要解析JSONArray,如下所示:

JSONArray jsonArray = new JSONArray(response);

如果您想按键读取数据,可以执行以下操作:

for (int index = 0; index < jsonArray.length(); index++) {
    JSONObject object = jsonArray.optJSONObject(index);
    if (object != null) {
        String name = object.optString("name");
        String number = object.optString("number");
        String entity = object.optString("entity");
    }
}

但是现在我使用Gson将数据解析为对象。见下。

步骤1:将以下行添加到Gradle配置

dependencies {
  compile 'com.google.code.gson:gson:2.8.0'
}

第2步:像这样定义UserModel类

import com.google.gson.annotations.Expose;
import com.google.gson.annotations.SerializedName;

public class UserModel {

    @SerializedName("name")
    @Expose
    private String name;
    @SerializedName("number")
    @Expose
    private String number;
    @SerializedName("entity")
    @Expose
    private String entity;

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getNumber() {
        return number;
    }

    public void setNumber(String number) {
        this.number = number;
    }

    public String getEntity() {
        return entity;
    }

    public void setEntity(String entity) {
        this.entity = entity;
    }

}

步骤3:在onResponse方法上更改为以下:

Gson gson = new GsonBuilder().create();
ArrayList<UserModel> models = gson.fromJson(response, new TypeToken<ArrayList<UserModel>>() {
}.getType());

现在您可以像这样访问您的数据

if (models != null && models.size() > 0) {
    for (int i = 0; i < models.size(); i++) {
        UserModel user = models.get(i);
        user.getName();
        user.getEntity();
        user.getNumber();
    }
}