How To Read Json Parent Tags In Android
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'}
Solution 1:
You should read the documentation for JSONObject.names
Basically, names()
(in Android) can be used to get an array of field names, and then use those as your keys. I won't do a full example here because you'll learn better if you try for yourself.
Solution 2:
publicstaticStringFechingdata(String response){
Id1.clear();
Id2.clear();
Id3.clear();
name1.clear();
try{
JSONArray jsonarray = newJSONArray("["+response+"]");
JSONObject jsonobject = jsonarray.getJSONObject(0);
parseForResponse1(jsonobject.getString("ebook"),1);
parseForResponse2(jsonobject.getString("ebrochure"),2);
parseForResponse3(jsonobject.getString("emenu"),3);
return response;
}catch (Exception e) {
return"\"" + "Access Denied" + "\"";
}
}
privatestaticvoidparseForResponse1(String res,int i) {
try{
JSONArray jsonarray1 = newJSONArray(res);
Log.d("jsarry1",""+jsonarray1.length());
for(int i=0;i<jsonarray1.length();i++){
JSONObject jsonobject = jsonarray1.getJSONObject(i);
if(i ==1){
Id1.add(jsonobject.getString("id"));
name1.add(jsonobject.getString("id"));
}
if(i ==2){
Id2.add(jsonobject.getString("id"));
name2.add(jsonobject.getString("id"));
}
if(i ==2){
Id3.add(jsonobject.getString("id"));
name3.add(jsonobject.getString("id"));
}
}
}
catch (Exception e) {
System.out.println(e.toString());
}
}
This try this json parsging
Solution 3:
JSONObject jobj = newJSONObject(YourJsonString);
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
Post a Comment for "How To Read Json Parent Tags In Android"