使用 volley 获取数组中的对象
Get object inside array with volley
我正在尝试检索 json 数组中的对象,例如 creditcard
中的 id
和 brand
。
{
"result_count": "2",
"total_no_of_rows": "2",
"status": {
"status_code": "-1",
"status_text": "Success"
},
"cards": [
{
"creditCard": {
"id": "CRC-9C7I2BGN8RJY",
"brand": "AMEX",
"first6": "376449",
"last4": "3005",
"store": true
},
"method": "CREDIT_CARD"
},
{
"creditCard": {
"id": "CRC-FXDA9E2YCWWQ",
"brand": "ELO",
"first6": "636297",
"last4": "7013",
"store": true
},
"method": "CREDIT_CARD"
}
]
}
我使用下面的请求没有成功,因为它 returns 卡没有值。
JsonObjectRequest req = new JsonObjectRequest(Request.Method.GET,
url, null, response -> {
arrayData.clear();
try {
JSONArray cards = response.getJSONArray("cards");
for (int i = 0; i < cards.length(); i++) {
JSONObject token = cards.getJSONObject(i);
CardList card = new CardList(
token.getString("id"),
token.getString("brand"),
token.getString("first6"),
token.getString("last4"),
token.getBoolean("store"));
arrayData.add(card);
}
Log.v("arrayData", String.valueOf(arrayData));
} catch (JSONException e) {
Log.v("arrayDataError", String.valueOf(e));
e.printStackTrace();
}
}, new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
Log.v("arrayDataError", String.valueOf(error));
}
});
有人可以帮忙吗?谢谢。
为有需要的人提供解决方案:
JSONArray cards = response.getJSONArray("cards");
for (int i = 0; i < cards.length(); i++) {
JSONObject token = cards.getJSONObject(i);
JSONObject creditCard = token.getJSONObject("creditCard");
card_method = token.getString("method").equals("CREDIT_CARD") ?
getString(R.string.creditcard) : getString(R.string.debitcard);
CardList card = new CardList(
creditCard.getString("id"),
creditCard.getString("brand"),
creditCard.getString("first6"),
creditCard.getString("last4"),
creditCard.getBoolean("store"));
arrayData.add(card);
}
我正在尝试检索 json 数组中的对象,例如 creditcard
中的 id
和 brand
。
{
"result_count": "2",
"total_no_of_rows": "2",
"status": {
"status_code": "-1",
"status_text": "Success"
},
"cards": [
{
"creditCard": {
"id": "CRC-9C7I2BGN8RJY",
"brand": "AMEX",
"first6": "376449",
"last4": "3005",
"store": true
},
"method": "CREDIT_CARD"
},
{
"creditCard": {
"id": "CRC-FXDA9E2YCWWQ",
"brand": "ELO",
"first6": "636297",
"last4": "7013",
"store": true
},
"method": "CREDIT_CARD"
}
]
}
我使用下面的请求没有成功,因为它 returns 卡没有值。
JsonObjectRequest req = new JsonObjectRequest(Request.Method.GET,
url, null, response -> {
arrayData.clear();
try {
JSONArray cards = response.getJSONArray("cards");
for (int i = 0; i < cards.length(); i++) {
JSONObject token = cards.getJSONObject(i);
CardList card = new CardList(
token.getString("id"),
token.getString("brand"),
token.getString("first6"),
token.getString("last4"),
token.getBoolean("store"));
arrayData.add(card);
}
Log.v("arrayData", String.valueOf(arrayData));
} catch (JSONException e) {
Log.v("arrayDataError", String.valueOf(e));
e.printStackTrace();
}
}, new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
Log.v("arrayDataError", String.valueOf(error));
}
});
有人可以帮忙吗?谢谢。
为有需要的人提供解决方案:
JSONArray cards = response.getJSONArray("cards");
for (int i = 0; i < cards.length(); i++) {
JSONObject token = cards.getJSONObject(i);
JSONObject creditCard = token.getJSONObject("creditCard");
card_method = token.getString("method").equals("CREDIT_CARD") ?
getString(R.string.creditcard) : getString(R.string.debitcard);
CardList card = new CardList(
creditCard.getString("id"),
creditCard.getString("brand"),
creditCard.getString("first6"),
creditCard.getString("last4"),
creditCard.getBoolean("store"));
arrayData.add(card);
}