android 循环输出数据,如何循环浏览android中的json数据

这篇博客讨论了如何在Android应用中循环访问和解析从服务器获取的JSON数据。作者遇到的问题在于原始JSON数据没有包含数组括号,因此无法直接用JSONArray处理。在回答的指导下,作者将JSON字符串转换为JSONArray,并通过for循环遍历每个对象,成功地访问到了每个JSON对象的‘places’字段。
摘要由CSDN通过智能技术生成

正如标题所述。我如何循环访问我从服务器获得的json数据。我要找到这样的JSON数据的如何循环浏览android中的json数据

{

"tag":"home",

"success":1,

"error":0,

"uid":"4fc8f94f1a51c5.32653037",

"name":"Saleem",

"profile_photo":"http:\/\/example.info\/android\/profile_photos\/profile1.jpg",

"places":

{

"place_photo":"http:\/\/example.info\/android\/places_photos\/place1.jpg",

"created_at":"2012-06-02 00:00:00",

"seeked":"0"

}

}

{

"tag":"home",

"success":1,

"error":0,

"uid":"4fc8f94f1a51c5.32653037",

"name":"Name",

"profile_photo":"http:\/\/example.info\/android\/profile_photos\/profile1.jpg",

"places":

{

"place_photo":"http:\/\/example.info\/android\/places_photos\/place1.jpg",

"created_at":"2012-06-02 00:00:00",

"seeked":"0"

}

}

{

"tag":"home",

"success":1,

"error":0,

"uid":"4fc8f94f1a51c5.32653037",

"name":"Name",

"profile_photo":"http:\/\/example.info\/android\/profile_photos\/profile1.jpg",

"places":

{

"place_photo":"http:\/\/example.info\/android\/places_photos\/place1.jpg",

"created_at":"2012-06-02 00:00:00",

"seeked":"0"

}

}

这里是我得到的json数据

public class Home extends Activity {

Button btnLogout;

ScrollView svHome;

UserFunctions userFunctions;

LoginActivity userid;

@Override

protected void onCreate(Bundle savedInstanceState) {

// TODO Auto-generated method stub

super.onCreate(savedInstanceState);

userid = new LoginActivity();

svHome = (ScrollView)findViewById(R.id.svHome);

setContentView(R.layout.home);

userFunctions = new UserFunctions();

/***********************************************************/

//here is where my above mentioned json data is

JSONObject json = userFunctions.homeData();

try {

if(json != null && json.getString("success") != null) {

//login_error.setText("");

String res = json.getString("success");

//userid = json.getString("uid").toString();

if(Integer.parseInt(res) == 1) {

//currently this only shows the first json object

Log.e("pla", json.toString());

} else {

//login_error.setText(json.getString("error_msg"));

}

} else {

Toast.makeText(getBaseContext(), "No data", Toast.LENGTH_LONG).show();

}

} catch (JSONException e) {

e.printStackTrace();

}

/*******************************************************/

}

}

更新

更改accroding在答案中给出的链接后。这里是我的变化

/***********************************************************/

JSONObject json = userFunctions.homeData();

String jsonData = json.toString();

try {

if(json != null && json.getString("success") != null) {

//login_error.setText("");

String res = json.getString("success");

//userid = json.getString("uid").toString();

if(Integer.parseInt(res) == 1) {

JSONArray jsonArray = new JSONArray(jsonData);

for (int i = 0; i < jsonArray.length(); i++) {

JSONObject jsonObject = jsonArray.getJSONObject(i);

Log.e("Object", jsonObject.getString("places"));

//Log.i(ParseJSON.class.getName(), jsonObject.getString("text"));

}

Log.e("pla", json.toString());

} else {

//login_error.setText(json.getString("error_msg"));

}

} else {

Toast.makeText(getBaseContext(), "No data", Toast.LENGTH_LONG).show();

}

} catch (JSONException e) {

e.printStackTrace();

}

2012-06-02

2619

+0

你可以在json中进行更改吗?因为json中没有数组括号“[”“]”? –

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值