如何迭代JSON对象?

时间:2018-03-15 16:41:17

标签: android json loops

MY JSON回复

{"数据":[{" UID":" 1"" PHONE_NUMBER":"数据" "名称":"数据""数":"""令牌":"数据""电子邮件":"数据""网站":"数据"" profile_picture&#34 ;: #&34;数据"},{" UID":" 2"" PHONE_NUMBER":"数据"&#34 ;名称":"数据""数":"数据""令牌":"数据" "电子邮件":"数据""网站":"数据"" profile_picture":"数据"}]}

在Android中,我有一个对象Friend.java

 jObject = new JSONObject(Response);
 if(!error){
          Toast.makeText(PasswordActivity.this, "Data Got Perfectly", Toast.LENGTH_LONG).show();
/*This Is Where Will Loop May Start*/
//loop{

          JSONObject object = jObject.getJSONObject("data");
          Friend friend = new   Friend(object.getString("name"),
                                       object.getString("number"),
                                       object.getString("token"),
                                       object.getString("email"),
                                       object.getString("website"); 
                                       object.getString("profile_picture"));
dbHelper.addFriend(friend);
//}
/*This Is Where Will Loop May End*/
}

如何在Android上迭代数据以在SQLite上保存数据;

1 个答案:

答案 0 :(得分:0)

使用这种方式data是一个数组

JSONArray jsonarray = jObject.getJSONArray("data");
for (int i = 0; i < jsonarray.length(); i++) {
JSONObject jsonobject = jsonarray.getJSONObject(i);
String name = jsonobject.getString("name");
String number = jsonobject.getString("number");
}