Pages

Monday 5 August 2013

Split the Json format.


Here i am going to show you how to split the json format.

toJson() – Convert Java object to JSON format
fromJson() – Convert JSON into Java object

sample format.

{"user_id":"1","email":"xxx@gmail.com","password":"12","name":"yyy","gender":"male"}

Step1:
  Create a class for this json example

Class Info
{
public String user_id;
public String email;
public String password;
public String name;
public String gender;
}

Step2:
Download the gson jar file and add it in your lib folder.to know more about gson and json.

Step3:
//fromJson is used to convert back to object and display it.

        String result = "Your json string"
Gson gson = new Gson();
Info getResponse = gson.fromJson(result, Info.class);
        String name = getResponse.name; //To get the value of name.
        Toast.makeText(getApplicationContext(),
getResponse.name, Toast.LENGTH_LONG)
.show();

No comments:

Post a Comment