Yash Kumar Atri
Yash Kumar Atri

Reputation: 795

JSONObject parsing in android?

The JSONObject looks like

{"result":
     {
        "id":"1",
        "name":"ankur",
        "email":"[email protected]",
        "address":"bblock",
        "designation":"devloper",
        "department":"development",
        "balanceleave":"5"
     }
}

and my parse code looks like

Intent intent = new Intent(Login.this, Profile.class);
intent.putExtra("id", response.getString("id"));
intent.putExtra("name", response.getString("name"));
intent.putExtra("email", response.getString("email"));
intent.putExtra("address", response.getString("address"));
intent.putExtra("designation", response.getString("designation"));
intent.putExtra("department", response.getString("department"));
intent.putExtra("balanceleave", response.getString("balanceleave"));

startActivity(intent);

Can you help me out, actually I am parsing the JSON and sending it to the profile activity to display and if you can comeup with something that I can send the JSONObject to profile that would be great!!

Upvotes: 1

Views: 56

Answers (3)

Sagar Patel
Sagar Patel

Reputation: 224

You can write below line

JSONString url = objectname.getJsonString("url");

then put in intent

Upvotes: 0

Akash
Akash

Reputation: 971

Try like this :

JSONObject response=new JSONObject("your response string");
JSONObject result=response.getJSONObject("result");
String id=result.getString("id");
String name=result.getString("name"));
String email=result.getString("email"));
String address=result..getString("address"));
String designation=result.getString("designation"));
String department=result.getString("department"));
String balanceleave=result.getString("balanceleave"));

Upvotes: 2

Mehul Kabaria
Mehul Kabaria

Reputation: 6622

yes you have to convert JsonObject to String and then send via Bundle to Profile Screen. Like below

Intent intent = new Intent(Login.this, Profile.class);
intent.putExtra("JSON_OBJECT", response.toString());


                                startActivity(intent);

To Get that

Intent i = getIntent();
String json = i.getStringExtra("JSON_OBJECT");
try{
JsonObject json = new JsonObject(json);
}catch(Exception e){
}

Upvotes: 0

Related Questions