从android中的数组列表逐个使用项目

时间:2016-07-04 07:47:35

标签: java android arrays json

解析时我遇到了麻烦。我有一个Json格式的数组列表。

    {
      "details": [
        {
          "question_id": "1",
          "Questions": "QUESTION1",
          "Marks": "10"
        },
        {
          "question_id": "2",
          "Questions": "QUESTION2",
          "Marks": "10"
        },
        {
          "question_id": "3",
          "Questions": "QUESTION3",
          "Marks": "10"
        }
      ],
      "status_code": 1,
      "status_description": "Successsfull."
    }

现在从这个JSONARRAY我需要从每个对象发送 question_id 到下一个活动,我需要在URL中发送 question_id 以获取下一个问题来自API。

我的问题是如何在另一项活动中逐一发送 question_id 。 实际上这是一个QUIZ APP。

4 个答案:

答案 0 :(得分:1)

首先使用getJSONArray细节获取JSONArray,然后获取。像这样尝试获取。

 class Load_Filterad extends AsyncTask<String, String, JSONObject>
    {

        String dis=filterdis.getText().toString();
        JSONParser jsonParser = new JSONParser();

        private static final String LOGIN_URL = "xxx";
        private static final String TAG_SUCCESS = "status_description";
        private static final String TAG_MESSAGE = "details";

        @Override
        protected void onPreExecute()
        {
            pd.show();
        }

        @Override
        protected JSONObject doInBackground(String... args) {

            try {

                HashMap<String, String> params = new HashMap<>();

                params.put("cat_id",finalcatid);
                params.put("district",dis);

                Log.d("request", "starting");

                JSONObject json = jsonParser.makeHttpRequest(
                        LOGIN_URL, "POST", params);

                if (json != null) {
                    Log.d("JSON result", json.toString());
                    result = json.getString(TAG_MESSAGE);
                    return json;
                }

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

            return null;
        }

        protected void onPostExecute(JSONObject json) {

            if (pd != null && pd.isShowing())
            {
                pd.dismiss();
            }

            if (json != null)
            {
                try
                {
                    filterdis.setText("");
                    result=json.getString("result");
                    if(result.equals("true"))
                    {

                        browseadarray=json.getJSONArray("product_result");
                        for (int i = 0; i < browseadarray.length(); i++)
                        {
                            JSONObject c = browseadarray.optJSONObject(i);
                            if (c != null)
                            {
                                HashMap<String, String> map = new HashMap<String, String>();
                                String advertise_id = c.getString("advertise_id");
                                String add_title = c.getString("add_title");
                                String cat_id = c.getString("cat_id");
                                String subcat_id = c.getString("subcat_id");
                                String add_price = c.getString("add_price");
                                String add_description = c.getString("add_description");
                                String add_name = c.getString("add_name");
                                String add_phone = c.getString("add_phone");
                                String add_city = c.getString("add_city");
                               // String add_district = c.getString("add_district");
                               // String add_taluka = c.getString("add_taluka");
                                String image_1 = c.getString("image_name");



                                map.put("advertise_id", advertise_id);
                                map.put("add_title", add_title);
                                map.put("cat_id", cat_id);
                                map.put("subcat_id", subcat_id);
                                map.put("add_price", add_price);
                                map.put("add_description", add_description);
                                map.put("add_name", add_name);
                                map.put("add_phone", add_phone);
                                map.put("add_city", add_city);
                                //map.put("add_district", add_district);
                                //map.put("add_taluka", add_taluka);
                                map.put("image_1", image_1);


                                arraylist_browsead.add(map);
                                System.out.println(arraylist_browsead);

                            }

                        }
                        browseAdvertiseAdapter = new BrowseAdvertiseAdapter(getActivity(), arraylist_browsead);
                        browseadlist.setAdapter(browseAdvertiseAdapter);
                        pd.dismiss();
                    }
                    else
                    {
                        Toast.makeText(getActivity(),"No Product Available",Toast.LENGTH_SHORT).show();
                    }
                }

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

            }

            else
            {
                Toast.makeText(getActivity(),"No Advertise Available...",Toast.LENGTH_SHORT).show();

            }
        }

    }

答案 1 :(得分:1)

public class McqQuestionBeanCheck implements Serializable {

private int question_id;
private String questions,marks;


public int getQuestion_id() {
    return question_id;
}
public void setQuestion_id(int question_id) {
    this.question_id = question_id;
}
public String getQuestions() {
    return questions;
}
public void setQuestions(String questions) {
    this.questions = questions;
}
public String getMarks() {
    return marks;
}
public void setMarks(String marks) {
    this.marks = marks;
}

}

使用此方法进行解析...

    public ArrayList<McqQuestionBeanCheck> mcqQuestionParsingCheck(String response) {

    ArrayList<McqQuestionBeanCheck> questions = new ArrayList<McqQuestionBeanCheck>();
    try {
        JSONObject jsonObject = new JSONObject(response);

        JSONArray questionsArray = jsonObject.getJSONArray("details");
        for (int i = 0; i < questionsArray.length(); i++) {
            JSONObject questionObject = questionsArray.getJSONObject(i);
            McqQuestionBeanCheck question = new McqQuestionBeanCheck();
            question.setQuestion_id(questionObject.getInt("question_id"));
            question.setQuestions(questionObject.getString("Questions"));
            question.setMarks(questionObject.getString("Marks"));

            questions.add(question);
        }

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

答案 2 :(得分:0)

我认为你应该将Json 解析为一个对象(你可以在你的项目中构建一个相关的POJO类,然后使用google Gson api)。然后通过循环详细信息列表将所有 id 放入列表中。然后将id列表传递给下一个活动或片段。

答案 3 :(得分:0)

首先解析json格式,如下所示:

String json = YOUR_JSON_TEXT;

// fetch details array in json data.
JSONArray json_array = new JSONObject(json).getJSONArray("details");

// make an array to save question id's,
int all_question_id[] = new int[json_array.length()];

// read question id's from json_array
for (int i = 0; i < json_array.length(); i++) {
    all_question_id[i] = json_array.getJSONObject(i).getInt("question_id");
}

// pass all of them, which are separated to destionation activity
Intent questions = new Intent();
questions.putExtra("ALL_QUESTION_ID", all_question_id);
startActivity(questions);

现在您在目标活动中只需要getExtras,例如:

Bundle extras = getIntent().getExtras();

int question_id[] = null;

if(extras!=null){
    question_id = extras.getIntArray("ALL_QUESTION_ID");
}