【问题标题】:android json object string display in textviewandroid json对象字符串在textview中显示
【发布时间】:2015-02-16 15:33:41
【问题描述】:

我正在尝试在从 json 数组中提取的文本视图中显示数据。

我得到了正确的服务器答案,第一个 toast 正确显示 de json array {"android":{"total":4}}

php页面上线http://www.2w.cl/apps/db/cargar_resultados.php

import java.util.ArrayList;
import java.util.List;

import org.apache.http.NameValuePair;
import org.apache.http.message.BasicNameValuePair;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import android.app.Activity;
import android.os.AsyncTask;
import android.os.Bundle;
import android.widget.TextView;
import android.widget.Toast;

public class SeeResults extends Activity{

    TextView texto;

    JSONArray android = null;
    JSONParser jsonParser = new JSONParser();
    private static String url = "http://www.2w.cl/apps/db/cargar_resultados.php/";
    private static final String TAG_OS = "android";
    private static final String TAG_CONSULTA = "total";

    public String idconsulta;

    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.see_results);
        idconsulta = getIntent().getStringExtra("test");

        TextView tv = null;
        tv = (TextView)findViewById(R.id.textView1);
        tv.setText(idconsulta);

        Toast.makeText(this, "On Create Called" + idconsulta, Toast.LENGTH_LONG).show();
    }

    @Override
    protected void onStart() {
        // TODO Auto-generated method stub
        super.onStart();
        new JSONParse().execute();
        Toast.makeText(this, "On Start Called", Toast.LENGTH_LONG).show();
    }


    private class JSONParse extends AsyncTask<String, String, JSONObject> {

       @Override
         protected void onPreExecute() {
             super.onPreExecute();

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

            // Building Parameters
            List<NameValuePair> params = new ArrayList<NameValuePair>();
            params.add(new BasicNameValuePair("idconsultas", idconsulta));
            JSONObject json = jsonParser.makeHttpRequest(url, "POST", params);

            return json;
        }

        @Override
          protected void onPostExecute(JSONObject json) {

            Toast.makeText(getBaseContext(), "test" + json, Toast.LENGTH_LONG).show();

          try {
             android = json.getJSONArray(TAG_OS);
             for(int i = 0; i < android.length(); i++){  
             JSONObject c = android.getJSONObject(i);

             String total = c.getString(TAG_CONSULTA);
             Toast.makeText(getBaseContext(), "test2" + total, Toast.LENGTH_LONG).show();
             TextView tv = null;
             tv = (TextView)findViewById(R.id.textView1);
             tv.setText(total);
             }
         } catch (JSONException e) {
           e.printStackTrace();
         }

        }
     }


}

toast "test2" 不显示任何内容。

感谢您的帮助

【问题讨论】:

  • 发布 logcat(并阅读它,您会看到它解释了您的 json 响应不是数组。)

标签: android arrays json string


【解决方案1】:

响应json字符串为:

{"android":{"total":4}}

其中androidJSONObject 而不是JSONArray 所以从json JSONObject 获取JSONObject:

JSONObject  android = json.getJSONObject(TAG_OS);
String total = android.optString(TAG_CONSULTA);

【讨论】:

  • 我按照说明进行了更改,但现在我无法使用 JSONObject c = android.getJSONObject(i);
  • @IngolfKrauss:看我的回答不需要for循环和其他代码
【解决方案2】:
  1. 调试时,使用“Log.d”而不是 Toasts
  2. 我会避免将 Toast 放入循环中
  3. 它未经测试,但试试这个代码:

    String result = "{\"android\":{\"total\":\"4\"}}";
    Log.d("something", "result: " + result);
    try {
        JSONObject hold = new JSONObject(result);
        JSONObject obj = hold.getJSONObject("android");
        Log.d("something", "total: " + obj.getString("total"));
    
    } catch (JSONException e) {
        Log.d("something", "ERROR");
    }
    

【讨论】:

  • 哦,你的数字 4 需要在双引号内 afaik
猜你喜欢
  • 1970-01-01
  • 2016-04-06
  • 1970-01-01
  • 1970-01-01
  • 2016-08-25
  • 1970-01-01
  • 2018-01-23
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多