0

As mentioned in the title. How can I loop through my json data that I am getting from server. My getting this kind of json data

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

here is where I am getting my json data

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

}

Update

After make changes accroding to the link given in the answer. Here are my changes

 /***********************************************************/
    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();
    }
1
  • can you make changes in json ? as there is no array braces "[" "]" in json ? Commented Jun 2, 2012 at 13:47

4 Answers 4

2

Please look over the link

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

an if possible made the changes in json, as there is no array braces "[" "]" in json and you need that to itrate in loop

json should be like that

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

}

Sign up to request clarification or add additional context in comments.

4 Comments

Can you give me some link where I can change my json to format you just mentioned? Thanks in advance
I think json is coming from server ............if so then it depends on server side language......
I am using php on the server side
create the array of the objects and then convert that in json array bin-co.com/php/scripts/array2json
0

You could use the json libraries like the following:

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

which allows you to read json data into array like this:

JSONArray jsonArray = new JSONArray([your json data]);

Try this tutorial: http://www.vogella.com/articles/AndroidJSON/article.html

2 Comments

please see my update above. I got a logcat after making changes according to the link which is given above
can you make changes in json ? as there is no array braces "[" "]" in json ?
0

one other way I'll recommend is Use GSON library, It is easy end pain less. for well formatted Json

Comments

0

As your logcat states, you're trying to convert a JSONObject to a 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

Try to debug the code - find out where the exception is thrown, and make a JSONObject there instead of a JSONArray.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.