如何访问JSON数组的元素

时间:2017-01-12 11:37:50

标签: android json android-volley

下面给出的是我的JSON,我想访问“trip” JSON数组,并希望将它放在数组列表中,以便我可以在微调器中使用它。如何直接访问JSON数组并将其用作spinner的ArrayList?

我的JSON:

        {
            "trips": [
                77
            ],
            "status": {
            "message": "Successfully fetched the Open trips ",
            "code": 200
            }
        }

我的活动课程:

    public class MainActivity extends AppCompatActivity implements Spinner.OnItemSelectedListener {
    private Spinner spinner;
    private ArrayList<String> trips;
    private JSONArray result;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        trips= new ArrayList<String>();
        this.spinner = (Spinner) findViewById(R.id.spinner);
        spinner.setOnItemSelectedListener(this);
        loadtrip();
    }
    public void loadtrip() {
        HashMap<String,String> params=new HashMap<String,String>();
        {
            params.put("systemId", "12");
            params.put("customerId", "3513");
            params.put("userId", "124");
            params.put("tripType", "Open");
        }
        JsonObjectRequest stringRequest = new JsonObjectRequest(Request.Method.POST,config.DATA_URL,  new JSONObject(params),
                new Response.Listener<JSONObject>() {
                    public void onResponse(JSONObject response) {
                        try {
                            result = response.getJSONArray(config.JSON_ARRAY);
                        } catch (JSONException e) {
                            e.printStackTrace();
                        }

                        spinner.setAdapter(new ArrayAdapter<String>(MainActivity.this, android.R.layout.simple_spinner_dropdown_item,trips));
                    }
                },
                new Response.ErrorListener() {
                    @Override
                    public void onErrorResponse(VolleyError error) {
                    }
                }) ;
        RequestQueue requestQueue = Volley.newRequestQueue(this);
        requestQueue.add(stringRequest);

    }
    public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
        String item = parent.getItemAtPosition(position).toString();
        Toast.makeText(parent.getContext(), "Selected: " + item, Toast.LENGTH_LONG).show();
    }
    public void onNothingSelected(AdapterView<?> parent) {


    }
    }

6 个答案:

答案 0 :(得分:2)

您可以直接从索引中获取json数组的值。

JSONArray array = yourJsonObject.getJSONArray("trips");
    for (int i=0; i<array.length(); i++) {
        int value = array.getInt(i);
    }

答案 1 :(得分:1)

    JSONArray array = yourJsonObject.getJSONArray("trips");
    for (int i=0; i<array.length(); i++) 
    {
        int value = array.getInt(i);
    }
   JSONObject objStates = yourJsonObject.getJSONObject(“status”);
   String msg= objStates.getString(“message”)
   Int code= objStates.getInt(“code”)

答案 2 :(得分:0)

尝试类似

的内容
  try {
         result = response.getJSONArray("trips");
         for(int i = 0; i < result.length(); i++){
                trips.add(String.valueOf(result.getInt(i)));
         } 
  } catch (JSONException e) {
           e.printStackTrace();
  }

答案 3 :(得分:0)

声明int value;并解析JSON

 new Response.Listener<JSONObject>() {
 public void onResponse(JSONObject response) {
   try {
    result = response.getJSONArray("trips");
    for (int i=0; i<result.length(); i++) {
    value = result.getInt(i);
    trips.add(String.valueOf(value));
    }
    } catch (JSONException e) {
       e.printStackTrace();
    }
 spinner.setAdapter(new ArrayAdapter<String>(MainActivity.this, android.R.layout.simple_spinner_dropdown_item,trips));

答案 4 :(得分:0)

当您使用config.JSON_ARRAY键解析代码时,config.JSON_ARRAY的值是什么。如果config.JSON_ARRAY =“trip”那么很好,并用你的config.JSON_ARRAY替换静态“trip”键,否则按照我的静态键进行解析。

只需使用以下代码更新您的代码:在此我解析了trip JSON数组并将所有项添加到tripList中。

string renameSuffix = "ap";
Match match = Regex.Match(filename, @"(.*)\." + renameSuffix + @"(\d*)$");

答案 5 :(得分:0)

试试这个对我有用。

    JSONObject object = jObj.getJSONObject(result);

    Iterator<?> iterator = object.keys();
    while (iterator.hasNext()) {
        String key = (String) iterator.next();
        ArrayList<JSONObject> value = new ArrayList<>();
        JSONArray jsonArray = object.getJSONArray(key);
        for (int i = 0; i < jsonArray.length(); i++) {
            value.add(jsonArray.getJSONObject(i));
        }

        System.out.println("key : " + key + " " + "value : " + value);
        hm.put(key, value);
    }