Created
August 30, 2014 06:55
-
-
Save manniru/b758a13d891aef160e83 to your computer and use it in GitHub Desktop.
Gson
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class Consumer | |
{ | |
private String name; | |
private int age; | |
private String email; | |
public String getName() { | |
return name; | |
} | |
public void setName(String name) { | |
this.name = name; | |
} | |
public int getAge() { | |
return age; | |
} | |
public void setAge(int age) { | |
this.age = age; | |
} | |
public String getEmail() { | |
return email; | |
} | |
public void setEmail(String email) { | |
this.email = email; | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
String json = "{ | |
[\"name\":\"Deepak\",\"age\":30,\"email\":\"[email protected]\"], | |
[\"name\":\"Dhanu\",\"age\":29,\"email\":\"[email protected]\"], | |
[\"name\":\"Ajay\",\"age\":30,\"email\":\"[email protected]\"], | |
}"; | |
Gson gson = new Gson() | |
Type newType = new TypeToken<ArrayList<Consumer>>(){}.getType(); | |
List<Consumer> models = gson.fromJson(json,newType); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Consumer consumer = new Consumer(); | |
consumer.setName("Deepak"); | |
consumer.setAge(30); | |
consumer.setEmail("[email protected]"); | |
Gson gson = new Gson(); | |
String json = gson.toJson(consumer); | |
// {"name":"Deepak","age":30,"email":"[email protected]"} | |
String jsonStr = {“name”:”Deepak”,”age”:30,”email”:”[email protected]”}; | |
Gson gson = new Gson(); | |
Consumer consumer = gson.fromJson(jsonStr, Consumer.class); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// http://deepaknenmini.com/akc/display?url=DisplayNoteIMPURL&reportId=4087&ownerUserId=deepak | |
// https://code.google.com/p/google-gson | |
{ | |
["name":"Deepak","age":30,"email":"[email protected]"], | |
["name":"Dhanu","age":29,"email":"[email protected]"], | |
["name":"Ajay","age":30,"email":"[email protected]"], | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** Created a consumers list **/ | |
List<Consumer> consumers = new ArrayList<Consumer>(); | |
/**Adding 3 consumer to the list**/ | |
consumers.add(new Consumer("Deepak",30,"[email protected]")); | |
consumers.add(new Consumer("Dhanu",29,"[email protected]")); | |
consumers.add(new Consumer("Ajay",30,"[email protected]")); | |
Gson gson = new Gson(); | |
String jsonStr = gson.toJson(consumers); | |
/** | |
{ | |
[“name”:”Deepak”,”age”:30,”email”:”[email protected]”], | |
[“name”:”Dhanu”,”age”:29,”email”:”[email protected]”], | |
[“name”:”Ajay”,”age”:30,”email”:”[email protected]”], | |
} | |
*/ |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment