-1
{   "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 字符串(编辑得更短,因为这里变得太长了)。

我正在尝试找到一种能够迭代每个组并使用它们创建切换按钮的方法。

所以我想要做的是创建一个切换按钮,其名称在“omrid”中,开/关状态为“status”。“grupp”最终用于将切换分类成组,但现在并不重要。

我对此进行了搜索和搜索,似乎没有人拥有我特定的 JSON 字符串组合,而且我对 JSON/Android SDK 有点菜鸟。

4

3 回答 3

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
于 2013-10-25T11:27:34.707 回答
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
}

希望能帮助到你。

于 2013-10-25T11:31:03.530 回答
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");
}
于 2013-10-25T11:33:27.210 回答