如何在JSON对象中获取JSON数组?

编程入门 行业动态 更新时间:2024-10-17 17:21:01
本文介绍了如何在JSON对象中获取JSON数组?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

这是我的JSON

{ "data": [ { "id": 1, "Name": "Choc Cake", "Image": "1.jpg", "Category": "Meal", "Method": "", "Ingredients": [ { "name": "1 Cup Ice" }, { "name": "1 Bag Beans" } ] }, { "id": 2, "Name": "Ice Cake", "Image": "dfdsfdsfsdfdfdsf.jpg", "Category": "Meal", "Method": "", "Ingredients": [ { "name": "1 Cup Ice" } ] } ] }

这就是我获取id的方法,这部分名称在获得第一部分时工作正常o f JSON

And this is how i am getting the id, name this part works fine in getting the first part of JSON

//getting whole json string JSONObject jsonObj = new JSONObject(jsonStr); //extracting data array from json string JSONArray ja_data = jsonObj.getJSONArray("data"); int length = jsonObj .length(); //loop to get all json objects from data json array for(int i=0; i<length; i++) { JSONObject jObj = ja_data.getJSONObject(i); Toast.makeText(this, jObj.getString("Name").toString(), Toast.LENGTH_LONG).show(); // getting inner array Ingredients JSONArray ja = jObj.getJSONArray("Ingredients"); int len = ja.length(); }

现在我正试图获得成分不确定如何这是我一直在尝试

Now i'm trying to get the ingredients not sure how to this is what i have been trying

//getting whole json string JSONObject jsonObj = new JSONObject(jsonStr); //extracting data array from json string JSONArray ja_data = jsonObj.getJSONArray("data"); int length = jsonObj .length(); //loop to get all json objects from data json array for(int i=0; i<length; i++) { JSONObject jObj = ja_data.getJSONObject(i); Toast.makeText(this, jObj.getString("Name").toString(), Toast.LENGTH_LONG).show(); // getting inner array Ingredients JSONArray ja = jObj.getJSONArray("Ingredients"); int len = ja.length(); // getting json objects from Ingredients json array for(int j=0; j<len; j++) { JSONObject json = ja.getJSONObject(j); Toast.makeText(this, json.getString("name").toString(), Toast.LENGTH_LONG).show(); } }

但是不行不通怎样才能得到它?

But Does does not work how can i get it ?

推荐答案

我想这会对你有帮助。

JSONObject jsonObj = new JSONObject(jsonStr); JSONArray ja_data = jsonObj.getJSONArray("data"); int length = jsonObj.length(); for(int i=0; i<length; i++) { JSONObject jsonObj = ja_data.getJSONObject(i); Toast.makeText(this, jsonObj.getString("Name").toString(), Toast.LENGTH_LONG).show(); // getting inner array Ingredients JSONArray ja = jsonObj.getJSONArray("Ingredients"); int len = ja.length(); ArrayList<String> Ingredients_names = new ArrayList<>(); for(int j=0; j<len; j++) { JSONObject json = ja.getJSONObject(j); Ingredients_names.add(json.getString("name").toString()); } }

更多推荐

如何在JSON对象中获取JSON数组?

本文发布于:2023-07-14 19:55:26,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1106675.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:数组   象中   如何在   JSON

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!