如何在没有数组名称的情况下传递jsonarray并在textview上显示响应

时间:2016-08-11 05:37:49

标签: android arrays json android-studio xml-parsing

如何在没有json名称的情况下传递JSONArray

[{
    "points": 411,
    "type": "C"
}, {
    "points": 1600,
    "type": "G"
}, {
    "points": 13540,
    "type": "I"
}]

点数显示在3个不同的textview上,不在listview

private class ProgressTask extends AsyncTask<String, Void, Boolean> {
        protected void onPreExecute() {

        }
        protected Boolean doInBackground(final String... args) {

            JSONParse jParser = new JSONParse();

            HashMap<String, String> hMap = new HashMap<>();
            hMap.put("strUserId", "1000000004");
            JSONArray jsonarray = jParser.getJSONFromUrl(url,hMap);

            // get JSON data from URL
            for (int i = 0; i < jsonarray.length(); i++) {
                try {
                    JSONObject c = (JSONObject) jsonarray.get(i);
                     cpoint=c.getString("points");
                     gpoint=c.getString("points");
                    ipoint=c.getString("points");

                    tpoint = cpoint+gpoint+ipoint;

                    Log.d("points", String.valueOf(points));
                    Log.d("type",type);
                } catch (JSONException e) {
                    e.printStackTrace();
                }
            }
            return null;
            }
        @Override
        protected void onPostExecute(final Boolean success) {
            txtTotalPoints.setText(tpoint);
            txtOwnPoints.setText(ipoint);
            txtClubPoints.setText(cpoint);
            txtVoucherPoints.setText(gpoint);

        }
    }

1 个答案:

答案 0 :(得分:1)

 try {
        JSONArray jsonArray = new JSONArray("[" +
                "" +
                "{" +
                "" +
                "points: 411," +
                "type: C" +
                "}," +
                "" +
                "{" +
                "" +
                "points: 1600," +
                "type: G" +
                "}," +
                "" +
                "{" +
                "" +
                "points: 13540," +
                "type: I" +
                "}" +
                "" +
                "]");
    // use below stirng
        String response_value = jsonArray.toString();
   for (int i = 0; i < jsonarray.length(); i++) {
JSONObject jsonobject = jsonarray.getJSONObject(i);
// use points
String mPoints = jsonobject.getString("points");

String mType = jsonobject.getString("type");
}
          } catch (JSONException e) {
        e.printStackTrace();
    }