{ "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
} ] }
以上是我的JSON字符串(编辑得更短,因为它在这里变得太长了。)
我正在尝试找到一种方法来迭代每个组并使用它们创建一个togglebutton。
所以我要做的是创建一个togglebutton,其名称在“omrid”中以“status”的开/关状态表示。 “grupp”最终将用于将切换分组,但现在不重要。
我搜索并搜索了这个,没有人似乎有我特定的JSON字符串组成,我有点像JSON / Android SDK的菜鸟。
答案 0 :(得分:2)
我会使用JSONObject
。在Android或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
}
输出:
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
答案 1 :(得分:0)
这是代码
JSONObject json = new JSONObject(String your_json_string);
JSONArray arr = json.getJSONArray("gb");
for(int i = 0;i<arr.length();i++)
{
JSONObject temp = arr.getJSONObject(i);
temp.getString("omrid");//use them as your needs
temp.getString("status");//use them as your needs
temp.getString("grupp");// use them as your needs
}
希望它有所帮助。
答案 2 :(得分:0)
使用以下代码进行迭代。
如下所示制作一个JSONParser类。
import java.io.BufferedReader;
import java.io.InputStream;
import java.io.InputStreamReader;
import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.impl.client.DefaultHttpClient;
import org.json.JSONObject;
public class JSONParser {
public JSONParser() {
}
JSONObject jObj;
String json;
InputStream is = null;
public JSONObject getJsonFromUrl(String url) {
// TODO Auto-generated method stub
try {
DefaultHttpClient httpClient = new DefaultHttpClient();
HttpPost httpPost = new HttpPost(url);
HttpResponse httpResponse = httpClient.execute(httpPost);
HttpEntity httpEntity = httpResponse.getEntity();
is = httpEntity.getContent();
} catch (Exception e) {
e.printStackTrace();
}
try {
BufferedReader reader = new BufferedReader(new InputStreamReader(is, "iso-8859-1"), 8);
StringBuilder sb = new StringBuilder();
String line = null;
while ((line = reader.readLine()) != null) {
sb.append(line + "\n");
}
is.close();
json = sb.toString();
System.out.println("Json String : " + json);
} catch (Exception e) {
e.printStackTrace();
}
try {
jObj = new JSONObject(json);
} catch (Exception e) {
e.printStackTrace();
}
return jObj;
}
}
在MainActivity中使用以下迭代。
JSONParser parser = new JSONParser();
JSONObject o = parser.getJsonFromUrl("yourjsonurl");
JSONArray array = o.getJSONArray("gb");
for (int i = 0; i < array.length(); i++){
JSONObject j = array.getJSONObject(i);
String omrid = j.getString("omrid");
String status= j.getString("status");
String grupp = j.getString("grupp");
}