How To Convert Json String To Coffee Object - Jackson Example
JSON stands for JavaScript object notation, is a lightweight text or string representation of an object in addition to apace becoming a pop information central format. Though it's pretty early on to tell that JSON is going to supplant XML every bit pop information interchange format, It is for certain providing an alternative. JSON stand upwards for information inwards ii format either an object or an array. JSON object is an unordered collection of cardinal in addition to value, similar to String representation of hash table. On the other hand, JSON Array is an ordered collection of values. The original departure between JSON Object and JSON array is at that spot representation. JSON object is started amongst left span { in addition to ends amongst correct span } and cardinal values are separated using a colon (:). On the other hand, JSON Array starts amongst left bracket [ in addition to ends amongst correct bracket ] in addition to each value is separated past times comma. By looking at structure, You tin flame write your JSON parser to parse JSON object or array to Java object, but yous don't bespeak to.
There are lot of opened upwards origin library inwards Java which provides tried in addition to tested agency of converting JSON String to Java object e.g. Jackson in addition to GSON. In this Java tutorial nosotros volition encounter representative of converting a JSON String to Java object using Jackson library
There are lot of opened upwards origin library inwards Java which provides tried in addition to tested agency of converting JSON String to Java object e.g. Jackson in addition to GSON. In this Java tutorial nosotros volition encounter representative of converting a JSON String to Java object using Jackson library
How to convert JSON String to Java object using Jackson
Java class e.g. User inwards this case, spell code required to convert JSON String to Java object is exactly ii lines inwards fromJson(String json) method.
This method takes an Jon String which stand upwards for a User object inwards JSON format in addition to convert it into Java User object. In this Java representative I cause got do User every bit nested static class for convenience, You may do a split top score class if needed.
This method takes an Jon String which stand upwards for a User object inwards JSON format in addition to convert it into Java User object. In this Java representative I cause got do User every bit nested static class for convenience, You may do a split top score class if needed.
import java.io.IOException;
import org.apache.log4j.Logger;
import org.codehaus.jackson.JsonParseException;
import org.codehaus.jackson.map.JsonMappingException;
import org.codehaus.jackson.map.ObjectMapper;
/*
* Java plan to convert JSON String into Java object using Jackson library.
* Jackson is real slowly to move in addition to require exactly ii lines of code to do a Java object
* from JSON String format.
*
* @author http://javarevisited.blogspot.com
*/
public class JsonToJavaConverter {
private static Logger logger = Logger.getLogger(JsonToJavaConverter.class);
public static void main(String args[]) throws JsonParseException
, JsonMappingException, IOException{
JsonToJavaConverter converter = new JsonToJavaConverter();
String json = "{\n" +
" \"name\": \"Garima\",\n" +
" \"surname\": \"Joshi\",\n" +
" \"phone\": 9832734651}";
//converting JSON String to Java object
converter.fromJson(json);
}
public Object fromJson(String json) throws JsonParseException
, JsonMappingException, IOException{
User garima = new ObjectMapper().readValue(json, User.class);
logger.info("Java Object created from JSON String ");
logger.info("JSON String : " + json);
logger.info("Java Object : " + garima);
return garima;
}
public static class User{
private String name;
private String surname;
private long phone;
public String getName() {return name;}
public void setName(String name) {this.name = name;}
public String getSurname() {return surname;}
public void setSurname(String surname) {this.surname = surname;}
public long getPhone() {return phone;}
public void setPhone(long phone) {this.phone = phone;}
@Override
public String toString() {
return "User [name=" + refer + ", surname=" + surname + ", phone="
+ band + "]";
}
}
}
Output:
2013-01-07 01:15:05,287 0 [main] INFO JsonToJavaConverter - Java Object created from JSON String
2013-01-07 01:15:05,287 0 [main] INFO JsonToJavaConverter - JSON String : {
"name": "Garima",
"surname": "Joshi",
"phone": 9832734651}
2013-01-07 01:15:05,287 0 [main] INFO JsonToJavaConverter - Java Object : User [name=Garima, surname=Joshi, phone=9832734651]
Dependency
As I said, You tin flame either move Jackson or Gson to convert JSON String to Java object in addition to inwards this Java tutorial nosotros cause got used Jackson library for JSON to Java object conversion. If yous are using Maven for dependency management than yous tin flame add together next dependency inwards POM.xml :
<dependency>
<groupId>org.codehaus.jackson</groupId>
<artifactId>jackson-xc</artifactId>
<version>1.9.11</version>
</dependency>
jackson-xc-1.9.11.jar
jackson-core-asl-1.9.11.jar
jackson-mapper-asl-1.9.11.jar
if yous are non comfortable or confused on adding multiple JAR files into classpath, thence encounter 5 ways to add together multiple JAR to Classpath inwards Java.
That's all on How to convert JSON String to Java object using Jackson library. Though, this is a piddling representative in addition to actual object could move to a greater extent than complex, it demonstrate the procedure of creating Java object from JSON String. You tin flame besides move other JSON library similar GSON instead of Jackson to convert JSON String to Java object. Full listing of JSON libraries are available on http://www.json.org/.
Further Learning
Master Java Web Services in addition to REST API amongst Spring Boot
REST API Design, Development & Management
tutorial)
P.S. - If yous desire to larn how to railroad train RESTful Web Services using Spring Framework, banking enterprise fit out Eugen Paraschiv's REST amongst Spring course. He has late launched the certification version of the course, which is sum of exercises in addition to examples to farther cement the existent footing concepts yous volition larn from the course.
Komentar
Posting Komentar