首页 > 解决方案 > 从json数组中获取元素

问题描述

[{"id":"1","capacity":"150","type":"conference","prix":"0","description":"description","image":""},{"id":"2","capacity":"200","type":"Formation","prix":"0","description":"desc"}]

如何在 Android 上获取此数组的值?

标签: androidjson

解决方案


试试这个代码:

try{
        String yourstring = "ADD YOUR JSON ARRAY HERE";
        JSONArray temp = new JSONArray(yourstring);

        for (int i = 0; i < temp.length(); i++) {
            JSONObject row = (JSONObject) temp.get(i);
            String id = row.getString("id");
            String capacity= row.getString("capacity");
            String type= row.getString("type");
            String prix= row.getString("prix");
            String description = row.getString("description");
           // do something with each variable 
        }
}catch(Exception e){
     e.printstacktrace()
}

推荐阅读