在我的安卓应用程序中,我有这个JSON,看起来像这样:
{ "first_name": "ankit",
"last_name": "anurag",
"mobile": "8105580517",
"interest_input": [13, 14],
"user_cards": [
[17, "DBS NUS Alumni Platinum Card "],
[19, "DBS NUS Debit Card "]
],
"email": "ankit_anurag@stragure.com"}
正如你所看到的,对于"user_cards"键,该值是一个二维数组,带有id和name。我想得到这两个。
我为此编写的代码在这里:
JSONArray existingCards = o.getJSONArray("user_cards");
exCards = new String[existingCards.length()][2];//populate ur Array with blanks to avoid entrance of null values
//fill up the blanks to prevent null
for(int i = 0; i < existingCards.length(); i++){
exCards[i][0] = "";
exCards[i][1] = "";
}
//fill up the actual data
for(int i = 0; i < existingCards.length(); i++){
String k = exCards[i][0];
String l = exCards[i][1];
Log.v("exC", k+" "+l);
}
但我无法检索数据。请帮忙
您必须先初始化每个"行"数组:
exCards[0] = new String[SIZE];
exCards[1] = new String[SIZE];
Yo可以在 foor-loop 中做到这一点