如何将JSON数组解析为微调框

时间:2019-06-01 12:25:07

标签: java android json

我想解析带有嵌套数组和对象的JSON文件,并使用解析后的信息填充微调框。 现在,我停留在进度窗口中,仅显示“正在加载” ...我只想将名称标签(navn)提取到微调器,并将nsrID存储在变量中,以备后用。

JSON格式:

[
  {
    "nsrId": "1018038",
    "koordinatLatLng": [
      59.6908,
      9.04228
    ],
    "navn": "Gransherad barnehage",
    "fylkesnummer": "08",
    "kommunenummer": "0807",
    "alder": "1 - 5",
    "eierform": "Kommunal",
    "antallBarn": 26
  },
  {
    "nsrId": "1012983",
    "koordinatLatLng": [
      59.5763,
      9.19806
    ],
    "navn": "Trolldalen Gårdsbarnehage",
    "fylkesnummer": "08",
    "kommunenummer": "0807",
    "alder": "1 - 5",
    "eierform": "Privat",
    "antallBarn": 24
  },

等...

我的代码:

    spinner = findViewById(R.id.barneHage_Spinner);

    hentJSON();
}

private void hentJSON() {
    showSimplProgressDialog(this, "loading...", "Henter Json", false);

    StringRequest stringRequest = new StringRequest(Request.Method.GET, barnehager_URL,
            new Response.Listener<String>() {
                @Override
                public void onResponse(String response) {
                    Log.d("strrrrr", ">>" + response);

                    try {
                        JSONObject obj = new JSONObject(response);

                        barnehageArrayList = new ArrayList<>();
                        JSONArray dataArray = obj.getJSONArray("navn");

                        for (int i = 0; i < dataArray.length(); i++) {
                            Barnehage model = new Barnehage();
                            JSONObject dataobj = dataArray.getJSONObject(i);

                            model.setNavn(dataobj.getString("navn"));
                           // model.setNsrId(dataArray.getNsrId(Integer.parseInt("nsrId")));

                            barnehageArrayList.add(model);
                        }

                        for (int i = 0; i < barnehageArrayList.size(); i++) {
                            names.add(barnehageArrayList.get(i).getNavn());
                        }
                        ArrayAdapter<String> spinnerArrayAdapter = new ArrayAdapter<String>(BarneHager.this, simple_spinner_item, names);
                        spinner.setAdapter(spinnerArrayAdapter);
                        removeSimpleProgressDialog();

                    } catch (JSONException e) {
                        e.printStackTrace();
                    }
                }

我希望输出结果是一个微调器,仅显示值

1: Gransherad barnehage
2: Trolldalen Gårdsbarnehage
3: ..
4: ..

2 个答案:

答案 0 :(得分:0)

您必须在列表的零位置添加一个项目

barnehageArrayList.add(0,new MODELCLASS("Select option"))

并添加以下代码以填充您的微调器

SPINNER_NAME.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
        @Override
        public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {

            if (position != 0) {

            } else {

            }
        }

        @Override
        public void onNothingSelected(AdapterView<?> parent) {

        }
    });

答案 1 :(得分:0)

我认为您通过JSONArray dataArray = obj.getJSONArray("navn")获取JSON数组时错了。

让我们尝试使用以下代码:

    @Override
    public void onResponse (String response) {
        try {
            JSONArray dataArray = new JSONArray(response);
            for (int i = 0; i < dataArray.length(); i++) {
                JSONObject dataobj = dataArray.getJSONObject(i);