解析json响应提取

时间:2015-10-19 11:28:38

标签: android json

// Reading json file from assets folder
        StringBuffer sb = new StringBuffer();
        BufferedReader br = null;
        try {
            br = new BufferedReader(new InputStreamReader(getAssets().open(
                    "boysquestion.json")));
            String temp;
            while ((temp = br.readLine()) != null)
                sb.append(temp);
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            try {
                br.close(); // stop reading
            } catch (IOException e) {
                e.printStackTrace();
            }
        }

        String myjsonstring = sb.toString();
        // Try to parse JSON

        String question = null;
        try {
            // Creating JSONObject from String
            JSONObject jsonObjMain = new JSONObject(myjsonstring);

            // Creating JSONArray from JSONObject
            JSONArray jsonArray = jsonObjMain.getJSONArray("category");

            // JSONArray has x JSONObject
            for (int i = 0; i < jsonArray.length(); i++) {

                // Creating JSONObject from JSONArray
                JSONObject jsonObj = jsonArray.getJSONObject(i);

                // Getting data from individual JSONObject
                question = jsonObj.getString("question");
                int no_score = jsonObj.getInt("no_score");
                int yes_score = jsonObj.getInt("yes_score");
                int category = jsonObj.getInt("category");


                Log.d("question boys", question);

                tvBoyGirl.setText(question);
                Log.d("random", question);

            }

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

Json Response,如何提取所有内容:

[{"category":1,"no_score":3,"question":"Does he ever send the first text of a conversation?","yes_score":1},{"category":1,"no_score":3,"question":"Does he reply with two or three word answers?","yes_score":1}]

上面是json的回复..我想提取每一个......所有类别,no_score,yes_score和问题..

我试过了

question = jsonObj.getString("question");

但我没有得到......任何人都可以帮助我提取。

3 个答案:

答案 0 :(得分:1)

Json object{开头,json array[开头。在你的情况下,json是一个数组。

因此将其作为数组而不是jsonobject读取。

即你需要阅读如下。

JSONArray jsonArray = new JSONArray(myjsonstring);

并遍历数组并读取每个值。

您的for loop看起来不错。

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

            JSONObject jsonObj = jsonArray.getJSONObject(i);
            question = jsonObj.getString("question");
            int no_score = jsonObj.getInt("no_score");
            int yes_score = jsonObj.getInt("yes_score");
            int category = jsonObj.getInt("category");

            tvBoyGirl.setText(question);

        }

答案 1 :(得分:1)

这就是我所做的一切

ListQuestion();


        tvQuestionText.setText(questions.get(i));


        imUp.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

                i++;
                question_counter++;

                tvQuestionCounter.setText(String.valueOf(question_counter));

                if (!questions.isEmpty()) {

                    tvQuestionText.setText(questions.get(i));

                    if (yes_Score1.get(i) == -1) {
                        score = score - 1;
                    } else {
                        score = score + yes_Score1.get(i);
                    }


                    System.out.println(score);
                    Log.d("yes_per", String.valueOf(score));


                    counter++;
                    if (counter == 25) {

                        calculation();
                    }
                }
            }
        });


        imDown.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

                i++;
                question_counter++;
                tvQuestionCounter.setText(String.valueOf(question_counter));
                if (!questions.isEmpty()) {

                    tvQuestionText.setText(questions.get(i));

                    if (no_Score1.get(i) == -1) {
                        score = score - 1;
                    } else {
                        score = score + no_Score1.get(i);
                    }

                    Log.d("no_per", String.valueOf(score));


                    System.out.println(score);

                    counter++;
                    if (counter == 25) {
                        calculation();
                    }
                }
            }
        });
    }

答案 2 :(得分:-1)

试试这个:

JSONArray jArray = new JSONArray(jsonString);
for(int i=0;i<jArray.length();i++)
{
  JSONObject jObj = jArray.getJSONObject(i);

  question = jObj.getString("question");
  int no_score = jObj.getInt("no_score");
  int yes_score = jObj.getInt("yes_score");
  int category = jObj.getInt("category");
}