1

I'm trying to get data once user logged in successfully but I never get any of results, what I am doing is next:

// response is my request to server
JSONObject obj = new JSONObject(response);
Log.d("RESPONSE",obj.toString());

so in log I do see values, like:

04-19 11:28:16.729: D/RESPONSE(3162): {"data":[{"loses":3,"username":"benedict","level":1,"strength":15,"experience":null,"gold":10,"password":"benedict","intelligence":5,"agility":10,"wins":5}],"status":true}

but once I try to read username for example like this:

String username = obj.getString("username");

The code above ^ gives me nothing in my string.. Any help how I can retrieve data from JSONObject? Thanks!

2
  • You need to learn JSON Parsing. Commented Apr 19, 2013 at 9:29
  • Yep, I indeed need to, first time trying and dont really have alot of time to go throught tutorial so I was trying here. If you know any good tutorial please suggest, I can google random too :) Commented Apr 19, 2013 at 9:32

4 Answers 4

4

That is because the username is present in the data object, which happens to be an JSONArray. Get the data array from the response object, traverse through each JSONObject in the array, and from each object, extract your username.

Something like this:-

JSONObject obj = new JSONObject(response);
JSONArray data = obj.getJSONArray("data");
for(int i=0;i<data.length();i++){
    JSONObject eachData = data.getJSONObject(i);
    System.out.println("Username= "+ eachData.getString("username"));
}
Sign up to request clarification or add additional context in comments.

2 Comments

Thank you very much I will try it right now, first time working with json :)
million thanks, its working fine! need to wait 2 mins then accept answer
2

your field username is in array data. To access into this try :

JSONObject obj = new JSONObject(response);
JSONArray array = obj.getJSONArray("data");

for(int i = 0; i < array.length(); ++i){
    JSONObject data = array.getJSONObject(i);
    String username = data.getString("username");
}

Comments

2

You need to first get JSONArray which is data :

    JSONArray data = null;
        data = json.getJSONArray("data");
         for(int i = 0; i < data.length(); i++){
                JSONObject c = data.getJSONObject(i);
         String username = c.getString("username");
}

You can get idea about parsing JSON from HERE

Comments

1

Try this...

try {

        JSONObject object = new JSONObject(response);
        JSONArray Jarray = object.getJSONArray("data");

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

           JSONObject Jasonobject = Jarray.getJSONObject(i);
           String loose= Jasonobject.getString("loses");
           String username=Jasonobject.getString("username");
          ....... 
          ........



        }

    } catch (JSONException e) {

        Log.e("log_txt", "Error parsing data " + e.toString());
    }

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.