在textview中显示android json对象字符串
我试图在文本视图中显示从json数组中提取的数据 我得到了正确的服务器答案,第一个toast正确地显示了de-json数组{“android”:{“total”:4} php页面是在线的在textview中显示android json对象字符串,android,arrays,json,string,Android,Arrays,Json,String,我试图在文本视图中显示从json数组中提取的数据 我得到了正确的服务器答案,第一个toast正确地显示了de-json数组{“android”:{“total”:4} php页面是在线的 import java.util.ArrayList; 导入java.util.List; 导入org.apache.http.NameValuePair; 导入org.apache.http.message.BasicNameValuePair; 导入org.json.JSONArray; 导入org.jso
import java.util.ArrayList;
导入java.util.List;
导入org.apache.http.NameValuePair;
导入org.apache.http.message.BasicNameValuePair;
导入org.json.JSONArray;
导入org.json.JSONException;
导入org.json.JSONObject;
导入android.app.Activity;
导入android.os.AsyncTask;
导入android.os.Bundle;
导入android.widget.TextView;
导入android.widget.Toast;
公共类SeeResults扩展活动{
TextView texto;
JSONArray-android=null;
JSONParser JSONParser=新的JSONParser();
专用静态字符串url=”http://www.2w.cl/apps/db/cargar_resultados.php/";
私有静态最终字符串标记_OS=“android”;
私有静态最终字符串标记_CONSULTA=“total”;
公共字符串idconsulta;
创建时受保护的void(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.see_results);
idconsulta=getIntent().getStringExtra(“测试”);
TextView tv=null;
tv=(TextView)findViewById(R.id.textView1);
tv.setText(idconsulta);
Toast.makeText(这个“在创建时”称为“+idconsulta,Toast.LENGTH_LONG).show();
}
@凌驾
受保护的void onStart(){
//TODO自动生成的方法存根
super.onStart();
新建JSONParse().execute();
Toast.makeText(这个“开始时调用”,Toast.LENGTH_LONG.show();
}
私有类JSONParse扩展了异步任务{
@凌驾
受保护的void onPreExecute(){
super.onPreExecute();
}
@凌驾
受保护的JSONObject doInBackground(字符串…args){
//建筑参数
List params=new ArrayList();
添加参数(新的BasicNameValuePair(“idconsultas”,idConsultta));
JSONObject json=jsonParser.makeHttpRequest(url,“POST”,参数);
返回json;
}
@凌驾
受保护的void onPostExecute(JSONObject json){
Toast.makeText(getBaseContext(),“test”+json,Toast.LENGTH_LONG.show();
试一试{
android=json.getJSONArray(TAG_OS);
对于(inti=0;i
toast“test2”没有显示任何内容
感谢您的帮助响应json字符串为: {“安卓”:{“总计”:4} 其中,
android
是JSONObject
而不是JSONArray
,因此从json
JSONObject获取JSONObject:
JSONObject android = json.getJSONObject(TAG_OS);
String total = android.optString(TAG_CONSULTA);
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");
}
我如前所述进行了更改,但现在我不能使用jsonobject c=android.getJSONObject(i)@IngolfKrauss:看我的答案不需要用于循环和其他codepost logcat(阅读它,你会看到它解释了你的json响应不是数组。)哦,你的数字4需要在双引号内
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");
}