I just started a career in programming, the following is a personal understanding, if some wrong, please indicate in the comments section, if there is a more detailed blog can recommend to me.
You must first create an entity classes based on JSON, and to implement the Serializable interface, and then create a JacksonUtils Tools
1 import java.io.Serializable; 2 3 public class Opst implements Serializable{ 4 5 6 private long dataCount; 7 private long mediaCount; 8 9 public long getDataCount() { 10 return dataCount; 11 } 12 13 public void setDataCount(long dataCount) { 14 this.dataCount = dataCount; 15 } 16 17 18 public long getmediacount () { 19 return mediacount; 20 } 21 22 public void setmediacount ( long mediacount) { 23 this .mediacount = mediacount; 24 } 25 26 }
1 import com.fasterxml.jackson.databind.ObjectMapper; 2 import com.fasterxml.jackson.databind.ObjectWriter; 3 4 import java.io.File; 5 import java.io.IOException; 6 import java.io.Writer; 7 8 public final class JacksonUtils { 9 10 private static final ObjectMapper OBJECT_MAPPER = new ObjectMapper(); 11 public static <T> T fromJsonString(String json, Class<T> clazz) { 12 if (json == null) 13 return null; 14 try { 15 return OBJECT_MAPPER.readValue(json, clazz); 16 } catch (Exception e) { 17 throw new RuntimeException("Unable to parse Json String.", e); 18 } 19 return null; 20 } 21 }
You can then deserialize, the following is the test code
import com.mhl.common.util.JacksonUtils;
1 public static void main(String[] args) { 2 String a="{\"dataCount\":29833,\"mediaCount\":4123}"; 3 Opst opst = JacksonUtils.fromJsonString(a, Opst.class); 4 System.out.println(opst.getDataCount()); 5 System.out.println(opst.getMediaCount()); 6 }
Output
29833
4123