android - 从json数组中获取元素
问题描述
[{"id":"1","capacity":"150","type":"conference","prix":"0","description":"description","image":""},{"id":"2","capacity":"200","type":"Formation","prix":"0","description":"desc"}]
如何在 Android 上获取此数组的值?
解决方案
试试这个代码:
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()
}
推荐阅读
- dialogflow-es - 如何通过 V2 客户端库为节点 js 创建 OutPutContext
- python - 在 Selenium 网页中单击“显示更多交易”
- javascript - 具有 8 个不同值集的对象
- ruby-on-rails - Webpacker:启用监视模式或取消挂钩 webpacker:从资产编译:Rails 中的预编译任务?
- macos - 完全卸载protobuf
- css - Django 应用程序不加载 CSS 背景图像,但图像加载内联样式。有解决办法吗?
- apache-spark - 对于 HIVE 查询,Is OR 提供比 IN 更好的性能
- python - 按行和列在 matplotlib 中迭代生成子图 - 仅绘制最终轴
- google-app-engine - 无法将 VS2017 GCP 应用程序部署到谷歌云 App Engine
- shell - Shell-Script:如果用户错误,则获取 cURL 错误