有没有办法从JSON输出中检索相应的数据,以在微调器中显示数据?

时间:2012-05-17 13:03:47

标签: android json

{
  "Platforms": [
    {
      "PlatformNo": "1",
      "SerialNumbers": [
        {
          "Aplilevel": "1",
          "Version": "1.0"
        },
        {
          "Aplilevel": "2",
          "Version": "1.1"
        },
        {
          "Aplilevel": "3",
          "Version": "1.5"
        },
        {
          "Aplilevel": "4",
          "Version": "1.6"
        }
      ]
    },
    {
      "PlatformNo": "2",
      "SerialNumbers": [
        {
          "Aplilevel": "5",
          "Version": "2.0"
        },
        {
          "Aplilevel": "6",
          "Version": "2.0.1"
        },
        {
          "Aplilevel": "7",
          "Version": "2.1.x"
        },
        {
          "Aplilevel": "8",
          "Version": "2.2.x"
        },
        {
          "Aplilevel": "9",
          "Version": "2.3"
        },
        {
          "Aplilevel": "10",
          "Version": "2.3.3"
        }
      ]
    },
    {
      "PlatformNo": "3",
      "SerialNumbers": [
        {
          "Aplilevel": "11",
          "Version": "3.0.x"
        },
        {
          "Aplilevel": "12",
          "Version": "3.1.x"
        },
        {
          "Aplilevel": "13",
          "Version": "3.2"
        }
      ]
    },
    {
      "PlatformNo": "4",
      "SerialNumbers": [
        {
          "Aplilevel": "14",
          "Version": "4.0"
        },
        {
          "Aplilevel": "15",
          "Version": "4.0.3"
        }
      ]
    }
  ]
}

上面显示的JSON是我需要在PlatformNo的微调器中显示的输出。当我选择一个项目时,我是否需要在另一个微调器中获得相应的API级别?任何人都可以帮助我解决这个问题吗?我正在尝试下面来得到它,但我无法弄清楚它。我只在下一个微调器中获取最后的值。有没有办法检查这个?请帮帮我。

String[] platfromno,apilevel;
Spinner splatform,sapilevel;  
JSONObject obj = json.getJSONObject("Result");
JSONArray platformObject = customerObject.getJSONArray("Platforms");
String parray = customerObject.getString("Platforms");
System.out.println("String of Platforms array is " + parray);

for (int i = 0; i < productNameObject.length(); i++) {
    JSONObject e = productNameObject.getJSONObject(i);
    platformno[i] = e.getString("PlatformNo");
    ArrayAdapter<String> spinnerPlatfromNo =
        new ArrayAdapter<String>( this,
                android.R.layout.simple_spinner_dropdown_item,
                platformno);

    splatform.setAdapter(spinnerPlatfromNo);
    JSONArray serialNumberObject = e.getJSONArray("SerialNumbers");
    apilevel= new String[serialNumberObject.length()];
    splatform.setOnItemSelectedListener(
        new OnItemSelectedListener() {
            public void onItemSelected(AdapterView<?> arg0,
                            View arg1, int arg2, long arg3) {
                System.out.println("selected Item position is  " +
                    splatform.getSelectedItemPosition());
            }

            public void onNothingSelected(AdapterView<?> arg0) {
                // TODO Auto-generated method stub
            }
    });

    // if (splatform.getSelectedItemPosition()) {

    for (int j = 0; j < serialNumberObject.length(); j++) {
        JSONObject e1 = serialNumberObject.getJSONObject(j);
        apilevel[j] = e1.getString("Aplilevel");
    // }
    }

1 个答案:

答案 0 :(得分:0)

  

经过多次努力,我解决了扭转者。

String[] platfromno,apilevel;
Spinner splatform,sapilevel;


    JSONObject obj = json.getJSONObject("Result");
    JSONArray platformObject = customerObject
                        .getJSONArray("Platforms");

                for (int i = 0; i < platformObject.length(); i++) {

                    JSONObject e = platformObject.getJSONObject(i);

                    platformno[i] = e.getString("PlatformNo");


                    ArrayAdapter<String> spinnerPlatfromNo = new ArrayAdapter<String>(
                            this,
                            android.R.layout.simple_spinner_dropdown_item,
                            platformno);

                    splatform.setAdapter(spinnerPlatfromNo);

                    JSONArray serialNumberObject = e
                            .getJSONArray("SerialNumbers");

                    apilevel= new String[serialNumberObject.length()];


                    splatform.setOnItemSelectedListener(new OnItemSelectedListener() {

                        public void onItemSelected(AdapterView<?> arg0,
                                View arg1, int arg2, long arg3) {
                            System.out.println("selected Item position is  "
                                    + splatform.getSelectedItemPosition());

                fillApiLevel();

                        }

                        public void onNothingSelected(AdapterView<?> arg0) {
                            // TODO Auto-generated method stub

                        }
                    });


        public void fillApiLevel(){

                JSONArray serialNumberObject = null;
        try {
            serialNumberObject =platformObject .getJSONObject(
                    splatform.getSelectedItemPosition()).getJSONArray(
                    "SerialNumbers");
        } catch (JSONException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }

        platfromno = new String[serialNumberObject.length()];
        apilevel = new String[serialNumberObject.length()];

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

            JSONObject e1 = null;
            try {
                e1 = serialNumberObject.getJSONObject(j);

                platfromno[j] = e1.getString("Aplilevel");
                apilevel [j] = e1.getString("Version");


            } catch (JSONException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }

            ArrayAdapter<String> spinnerSerialNumer = new ArrayAdapter<String>(
                    this, android.R.layout.simple_spinner_dropdown_item,
                    platformno);

            sSerialNo.setAdapter(spinnerSerialNumer);
                    }