{ "gb": [
{
"omrid": "gis-n",
"status": 0,
"grupp": 1
},
{
"omrid": "gis-s",
"status": 0,
"grupp": 1
},
{
"omrid": "gis-c",
"status": 0,
"grupp": 1
},
{
"omrid": "gis-h",
"status": 0,
"grupp": 1
},
{
"omrid": "gis-g",
"status": 0,
"grupp": 1
},
{
"omrid": "hes",
"status": 0,
"grupp": 2
} ] }
This above is my JSON String (edited shorter because it became too long here).
I'm trying to find a way to be able to iterate each group and create a togglebutton with them.
So what I'm trying to do is create a togglebutton with the name stated in "omrid" with the on/off status of "status". The "grupp" is for future use eventually to sort the toggles into groups but not important now.
I have searched and searched about this and noone seems to have my specific JSON string composition and I'm a bit of a noob with JSON/Android SDK.
I would use JSONObject
. THer is no difference where you run in on Android or PC.
String str = "{" +
" \"gb\": [" +
" {" +
" \"omrid\": \"gis-n\"," +
" \"status\": 0," +
" \"grupp\": 1" +
" }," +
" {" +
" \"omrid\": \"gis-s\"," +
" \"status\": 0," +
" \"grupp\": 1" +
" }," +
" {" +
" \"omrid\": \"gis-c\"," +
" \"status\": 0," +
" \"grupp\": 1" +
" }," +
" {" +
" \"omrid\": \"gis-h\"," +
" \"status\": 0," +
" \"grupp\": 1" +
" }," +
" {" +
" \"omrid\": \"gis-g\"," +
" \"status\": 0," +
" \"grupp\": 1" +
" }," +
" {" +
" \"omrid\": \"hes\"," +
" \"status\": 0," +
" \"grupp\": 2" +
" }" +
" ]" +
"}";
JSONObject jsonObject = new JSONObject(str);
JSONArray gb = jsonObject.getJSONArray("gb");
for (int j = 0; j < gb.length(); j++) {
JSONObject element = gb.getJSONObject(j);
int status = element.getInt("status");
int grupp = element.getInt("grupp");
String omrid = element.getString("omrid");
System.out.println("status=" + status + "; grupp=" + grupp + "; omrid=" + omrid);
//create togglebutton here
}
Output:
status=0; grupp=1; omrid=gis-n
status=0; grupp=1; omrid=gis-s
status=0; grupp=1; omrid=gis-c
status=0; grupp=1; omrid=gis-h
status=0; grupp=1; omrid=gis-g
status=0; grupp=2; omrid=hes