0

如标题中所述。如何遍历从服务器获取的 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

4 回答 4

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"
        }
    }
]

}

于 2012-06-02T13:49:53.767 回答
0

您可以使用如下 json 库:

导入 org.json.JSONArray;导入 org.json.JSONObject;

它允许您将 json 数据读入数组,如下所示:

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

试试这个教程:http ://www.vogella.com/articles/AndroidJSON/article.html

于 2012-06-02T13:30:17.580 回答
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。

于 2012-06-02T13:51:13.563 回答
0

我推荐的另一种方法是使用GSON库,它很容易结束痛苦。对于格式良好的 Json

于 2012-06-02T13:52:16.877 回答