Android 如何从json数组访问双json对象?
我有以下响应,我想在invidualAndroid 如何从json数组访问双json对象?,android,json,jsonobject,Android,Json,Jsonobject,我有以下响应,我想在invidualTextView中显示所有eqid,但我不知道如何实现这一点, 下面是我从服务器获取数据并显示在textview中的代码片段和响应。 有人能帮我吗 { "earthquakes":[ { "eqid":"c0001xgp", }, { "eqid":"c000905e", }, { "eqid":"2007hear", }, { "eqid":"c
TextView
中显示所有eqid
,但我不知道如何实现这一点,
下面是我从服务器获取数据并显示在textview中的代码片段和响应。有人能帮我吗
{
"earthquakes":[
{
"eqid":"c0001xgp",
},
{
"eqid":"c000905e",
},
{
"eqid":"2007hear",
},
{
"eqid":"c00090da",
},
{
"eqid":"2007aqbk",
},
{
"eqid":"2007hec6",
},
{
"eqid":"a00043nx",
},
{
"eqid":"2010utc5",
},
]
}
代码
ServiceHandler sh=newservicehandler();
jsonStr=sh.makeServiceCall(用户URL,ServiceHandler.GET);
Log.d(“响应:”、“>”+jsonStr);
试一试{
JSONObject json=新的JSONObject(jsonStr);
JSONArray items=json.getJSONArray(“产品”);
父布局=(RelativeLayout)findViewById(R.id.rl);
对于(int i=0;i您有一个JSON对象,其中包含一个JSON数组。因此,您首先从该对象检索数组,然后可以在该数组上循环以检索所有项。在下面的代码中,我假设您在一个数组中有预定的TextView
对象
try{
JSONObject json = new JSONObject(jsonstr);
JSONArray items = json.getJSONArray("earthquakes");
for(int i =0; i<items.length(); i++){
textViews[i].setText(items.getJSONObject(i).getString('eqid'));
}
}catch(JSONException e){
e.printStacktrace();
}
试试看{
JSONObject json=新的JSONObject(jsonstr);
JSONArray items=json.getJSONArray(“地震”);
对于(int i=0;i
试试看{
JSONObject json=新的JSONObject(jsonstr);
JSONArray items=json.getJSONArray(“地震”);
对于(int i=0;我能告诉你一件事吗?这个问题他们会改变颜色。我能不能交替添加按钮这将是一个单独的问题。问题应该停留在主题上。数组类型预期找到android小部件textviewshows@Roman在我的回答中,我声明我假设目标TextView
对象位于一个数组中,因此可以像textview[i]一样被引用
在循环中。如果textView
变量未引用textView
数组,则会出现此错误。我假设您知道在哪里可以找到textView
您想要放置数据。请查看我编辑的代码。我没有任何错误。但由于您使用了相对布局,因此不会显示响应textview将相互重叠…尝试线性布局或添加layoutToLeftOf或以上或以下等属性。。。。
try{
JSONObject json = new JSONObject(jsonstr);
JSONArray items = json.getJSONArray("earthquakes");
for(int i =0; i<items.length(); i++){
textViews[i].setText(items.getJSONObject(i).getString('eqid'));
}
}catch(JSONException e){
e.printStacktrace();
}