如何读取Android的JSON父标签 [英] How to read JSON parent tags in Android
问题描述
我需要以下件事上的帮忙。我想读 JSON 文件的父标签。下面是我的 JSON 文件内容;
{
电子书:
[
{ID:1,名:BOOK1},
{ID:2,名:BOOK2}
]
电子手册:
[
{ID:1,名:brochure1},
{ID:2,名:brochure2}
]
emenu:
[
{ID:1,名:MENU1},
{ID:2,名:MENU2}
]
}
我只需要找出父母多少标签在 JSON 并它们是什么。根据我给出的 JSON 文件的答案应该是 3 的(有3个父JSON变量)和电子书的电子手册, emenu 的(父标签的名称)。被疗法任何Java类,我可以让我的工作怎么办?
我知道如何读一个完整的 JSON 文件。首先我们通过在 JSON 到的JSONObject
键,然后通过孩子去 JSONArray
使用 getJSONArray()
方法。
但 getJSONArray()
方法需要父标签的名称。根据我的例子也可以是电子书的电子手册或 emenu 的。假设有一个情况下,我们看不到的 JSON 只能访问URL的内容。在这种情况下,我需要找出有多少家长标签是有,什么是他们的名字。
这是我的问题。在此先感谢我的帮助:-)
的JSONObject jobj =新的JSONObject(你的JSON字符串);
JSONArray jarray = jobj.names(); 的for(int i = 0; I< jarray.length();我++){ Log.e(parenttag,jarray.getString(ⅰ)); }
该U将得到所有的父标签u有在你的jsonString
I need your help with following matter. I want to read the parent tags of a JSON file. Below is my JSON file content;
{
"ebook":
[
{"id":"1","name":"book1"},
{"id":"2","name":"book2"}
],
"ebrochure":
[
{"id":"1","name":"brochure1"},
{"id":"2","name":"brochure2"}
],
"emenu":
[
{"id":"1","name":"menu1"},
{"id":"2","name":"menu2"}
]
}
I just need to find out how many parents tags in the JSON and what are they. According to my given JSON file the answers should be 3 (there are 3 parent JSON tags) and "ebooks, ebrochure, emenu" (names of the parent tags). Is ther any JAVA class I can get my work done?
I know how to read a full JSON file. First we pass the JSON to a JSONObject
and then pass children to a JSONArray
by using getJSONArray()
method.
But getJSONArray()
method needs the name of the parent tag. According to my example it can be ebook, ebrochure or emenu. Assume there is a circumstance where we can't see the content of the JSON only access the URL. In such a situation I need to find out how many parents tags are there and what are the names of them.
That's my problem. Thanks in advance for helping me :-)
JSONObject jobj = new JSONObject(Your Json String);
JSONArray jarray=jobj.names();
for(int i=0;i<jarray.length();i++){
Log.e("parenttag",jarray.getString(i));
}
by this u will get all parent tag u have in your jsonString
这篇关于如何读取Android的JSON父标签的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!