Android学習ノート_14_JSON形式データの処理

1670 ワード

public class ParseJsonTest extends AndroidTestCase{



    public void testJson() throws Exception {

        String userlist = "[{id:1001,name:\"tom\",age:22},{id:1002,name:\"tom1\",age:26}]";

        List<Person> persons = new ArrayList<Person>();

        JSONArray jsonArray = new JSONArray(userlist);

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

            JSONObject object = jsonArray.getJSONObject(i);

            Person person = new Person(object.getInt("id"),

                    object.getString("name"), object.getInt("age"));

            persons.add(person);

        }

        for (Person person : persons) {

            Log.i("ParseJsonTest", person.toString());

        }

    }

}