Java 使用 JSON OBJECT 解析 JSON 数组

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/19353581/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-12 16:24:02  来源:igfitidea点击:

Parsing JSON Array with JSON OBJECT

javaandroidjson

提问by Aoyama Nanami

I have some JSON with the following structure:

我有一些具有以下结构的 JSON:

{"cit": [
    "ALL",
    "Aceh Barat",
    "Aceh Besar",
    "Aceh Jaya",
    "Aceh Selatan",
    "Aceh Tengah",
    "Aceh Timur",
    "Aceh Utara"]}

i have try to parsing my json like this :

我尝试像这样解析我的 json:

JSONObject jsonObject = new JSONObject(result);
JSONArray city=jsonObject.getJSONArray("cit");

for (int j=0; j < city.length(); j++){
    JSONObject cit = city.getJSONObject(j);
    String kot = cit.toString(j);

    kota.add(kot);
}

on post execute :

在执行后:

ArrayAdapter<String> SpinnerKota = new ArrayAdapter<String>(Pencarian.this, android.R.layout.simple_list_item_1, kota);
spin_kota.setAdapter(SpinnerKota);  

but nothing happen, is there any wrong with my code? i hope someone can help me to solve my problem.

但什么也没发生,我的代码有什么问题吗?我希望有人能帮助我解决我的问题。

采纳答案by Raghunandan

"cit": [ // json array cit
"ALL",  // index 0 is ALL

Also there is no json object inside json array cit. So you don't need this JSONObject cit = city.getJSONObject(j).

json 数组 cit 中也没有 json 对象。所以你不需要这个JSONObject cit = city.getJSONObject(j)

Change

改变

  String kot = cit.toString(j);

To

  String kot =  (String) city.get(j);

Use the below

使用以下

      JSONObject jsonObject = new JSONObject("myjson string");
      JSONArray city=jsonObject.getJSONArray("cit");
      for(int i=0;i<city.length();i++)
      {
            String cities = (String) city.get(i);
            Log.i("All Cities",cities);
            kota.add(cities);
      }

回答by KOTIOS

Object obj = yourJsonResult;
JSONObject jsonObject = (JSONObject) obj;
JSONArray msg = (JSONArray) jsonObject.get("cit");
Iterator<String> iterator = msg.iterator();

while (iterator.hasNext()) {
    System.out.println(iterator.next());
}

回答by Sanath

you can do it as following code this is tested

您可以按照以下代码进行测试

String jsonSource="{'cit': ['ALL','Aceh Barat','Aceh Besar','Aceh Jaya','Aceh Selatan','Aceh Tengah','Aceh Timur','Aceh Utara']}";
    try {
        JSONObject jsonObject=new JSONObject(jsonSource);
        JSONArray jsonArray=jsonObject.getJSONArray("cit");         
        int length = jsonArray.length();
        String [] cities = new String [length];
        if (length > 0) {
            for (int i = 0; i < length; i++) {
                cities[i] = jsonArray.getString(i);
            }
        }
        //to check we can print our string array
        for (int i = 0; i < cities.length; i++) {
            Log.d("JSON parsing ",cities[i]);               
        }
    } catch (JSONException e) {
        e.printStackTrace();
    }

回答by Shashank Agarwal

Do it like this

像这样做

    JSONArray json = jParser.getJSONFromUrl(URL);

JSONObject c = json.getJSONObject(0);

JSONArray city  = c.getJSONArray("cit");    

for (int j=0; j < city.length(); j++){

    String kot = cit.get(j);

    kota.add(kot);
}