【问题标题】:How to pass jsonarray without array name and show response on textview如何传递没有数组名称的jsonarray并在textview上显示响应
【发布时间】:2016-12-17 16:48:25
【问题描述】:

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

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

点显示在 3 个不同的文本视图上,而不是在列表视图上

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);

        }
    }

【问题讨论】:

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


    【解决方案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();
        }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2012-04-27
      • 1970-01-01
      • 2017-05-24
      • 1970-01-01
      • 1970-01-01
      • 2017-03-04
      • 1970-01-01
      • 2021-05-28
      相关资源
      最近更新 更多