如何在android中循环遍历json数据

时间:2012-06-02 13:21:51

标签: java android json jsonp

如标题中所述。如何遍历我从服务器获取的json数据。我得到这种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();
    }
    /*******************************************************/
}

}

更新

根据答案中给出的链接进行更改后。这是我的更改

 /***********************************************************/
    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();
    }

4 个答案:

答案 0 :(得分:2)

请查看链接

http://www.androidhive.info/2012/01/android-json-parsing-tutorial/

如果可能的话,在json中进行了更改,因为在json中没有数组括号“[”“]”,你需要在循环中进行操作

json应该是那样的

{
"arrayKey": [
    {
        "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": "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"
        }
    }
]

}

答案 1 :(得分:0)

您可以使用如下的json库:

import org.json.JSONArray; import org.json.JSONObject;

允许您将json数据读入数组:

JSONArray jsonArray = new JSONArray([你的json数据]);

试用本教程:http://www.vogella.com/articles/AndroidJSON/article.html

答案 2 :(得分:0)

正如您的logcat所述,您正在尝试将JSONObject转换为JSONArray:

 org.json.JSONException: Value {"uid":"4fc8f94f1a51c5.32653037","places":{"place_photo":"http://example.info/android/places_photos/place1.jpg","created_at":"2012-06-02 00:00:00","seeked":"0","longitude":"24.943514","latitude":"60.167112"},"error":0,"success":1,"tag":"home","profile_photo":"http://example.info/android/profile_photos/profile1.jpg","name":"Zafar Saleem"} of type org.json.JSONObject cannot be converted to JSONArray

尝试调试代码 - 找出引发异常的位置,并在那里创建一个JSONObject而不是JSONArray。

答案 3 :(得分:0)

我推荐的另一种方法是使用GSON库,这很容易减轻痛苦。格式良好的Json