JSON can't read, key reading fail maybe

Posted by Abdullah Al Mubarok on Stack Overflow See other posts from Stack Overflow or by Abdullah Al Mubarok
Published on 2012-06-25T02:59:41Z Indexed on 2012/06/25 3:16 UTC
Read the original article Hit count: 145

Filed under:
|

I wonder why I can't read the JSON Object like this :

{
    "1":{"bulan":"Januari","tahun":"2012","tagihan":"205000","status":"Lunas"},
    "2":{"bulan":"Februari","tahun":"2012","tagihan":"180000","status":"Lunas"},
    "3":{"bulan":"Maret","tahun":"2012","tagihan":"120000","status":"Lunas"},
    "4":{"bulan":"April","tahun":"2012","tagihan":"230000","status":"Lunas"},
    "5":{"bulan":"Mei","tahun":"2012","tagihan":"160000","status":"Lunas"},
    "6":{"bulan":"Juni","tahun":"2012","tagihan":"150000","status":"Belum Lunas"},
    "panjang":6
}

with my android code like this :

    try {
        int length = jobj.getInt("panjang");

        for(int n = 0; n < length; n++){

            String m = Integer.toString(n)
            JSONObject row = jobj.getJSONObject(m);

            String bulan = row.getString("bulan");
            String tahun = row.getString("tahun");
            String tagihan = row.getString("tagihan");
            String status = row.getString("status");

            HashMap<String, String> map = new HashMap<String, String>();

            map.put("bulan", bulan);
            map.put("tahun", tahun);
            map.put("tagihan", tagihan);
            map.put("status", status);

            list.add(map);
        }
    } catch (JSONException e) {
        e.printStackTrace();
    }

It always return nothing, but it works fine if I change the key m to specific key like if

String m = "1";

and I can't use

JSONObject row = jobj.getJSONObject(n);

because getJSONObject() just accept string, not int. is there something wrong with my code?

© Stack Overflow or respective owner

Related posts about android

Related posts about JSON