当前位置:免费学习网考试资料计算机类内容页

如何使用Jackson解析JSON示例

2021-04-21 04:55:44 计算机类 访问手机版

  如何使用Jackson解析JSON示例

  导语:为了方便大家Java的.学习,下面是小编给大家提供的如何使用Jackson解析JSON示例,大家可以参考阅读,更多详情请关注免费学习网。

  package com.bbs.utils.json;

  import java.io.IOException;

  import java.util.ArrayList;

  import java.util.HashMap;

  import java.util.Iterator;

  import java.util.LinkedHashMap;

  import java.util.List;

  import java.util.Map;

  import java.util.Set;

  import org.codehaus.jackson.JsonGenerator;

  import org.codehaus.jackson.JsonParseException;

  import org.codehaus.jackson.map.JsonMappingException;

  import org.codehaus.jackson.map.ObjectMapper;

  import org.codehaus.jackson.node.JsonNodeFactory;

  public class JacksonTest

  private static JsonGenerator jsonGenerator = null;

  private static ObjectMapper objectMapper = null;

  private static User user = null;

  public static void writeEntity2Json throws IOException

  System.out.println"使用JsonGenerator转化实体为json串-------------";

  // writeObject可以转换java对象,eg:JavaBean/Map/List/Array等

  jsonGenerator.writeObjectuser;

  System.out.println;

  System.out.println"使用ObjectMapper-----------";

  // writeValue具有和writeObject相同的功能

  objectMapper.writeValueSystem.out, user;

  public static void writeList2Json throws IOException

  List userList = new ArrayList;

  userList.adduser;

  User u = new User;

  u.setUid10;

  u.setUname"archie";

  u.setUpwd"123";

  userList.addu;

  objectMapper.writeValueSystem.out, userList;

  public static void writeMap2Json

  try

  Map map = new HashMap;

  User u = new User;

  u.setUid10;

  u.setUname"archie";

  u.setUpwd"123";

  map.put"uid", u.getUid;

  map.put"uname", u.getUname;

  map.put"upwd", u.getUpwd;

  System.out.println"jsonGenerator";

  jsonGenerator.writeObjectmap;

  objectMapper.writeValueSystem.out, map;

  catch IOException e

  e.printStackTrace;

  public static void writeOthersJSON

  try

  String[] arr = "a", "b", "c" ;

  System.out.println"jsonGenerator";

  String str = "hello world jackson!";

  // byte

  jsonGenerator.writeBinarystr.getBytes;

  // boolean

  jsonGenerator.writeBooleantrue;

  // null

  jsonGenerator.writeNull;

  // float

  jsonGenerator.writeNumber2.2f;

  // char

  jsonGenerator.writeRaw"c";

  // String

  jsonGenerator.writeRawstr, 5, 10;

  // String

  jsonGenerator.writeRawValuestr, 5, 5;

  // String

  jsonGenerator.writeStringstr;

  jsonGenerator.writeTreeJsonNodeFactory.instance.POJONodestr;

  System.out.println;

  // Object

  jsonGenerator.writeStartObject;//

  jsonGenerator.writeObjectFieldStart"user";// user:

  jsonGenerator.writeStringField"name", "jackson";// name:jackson

  jsonGenerator.writeBooleanField"sex", true;// sex:true

  jsonGenerator.writeNumberField"age", 22;// age:22

  jsonGenerator.writeEndObject;

  jsonGenerator.writeArrayFieldStart"infos";// infos:[

  jsonGenerator.writeNumber22;// 22

  jsonGenerator.writeString"this is array";// this is array

  jsonGenerator.writeEndArray;// ]

  jsonGenerator.writeEndObject;//

  User u = new User;

  user.setUid5;

  user.setUname"tom";

  user.setUpwd"123";

  user.setNumber3.44;

  // complex Object

  jsonGenerator.writeStartObject;//

  jsonGenerator.writeObjectField"uid", u;// user:bean

  jsonGenerator.writeObjectField"infos", arr;// infos:[array]

  jsonGenerator.writeEndObject;//

  catch Exception e

  e.printStackTrace;

  /**

  * JSON字符串转换为对象

  */

  public static void readJson2Entity

  String json = "\"uid\":5,\"uname\":\"tom\",\"number\":3.44,\"upwd\":\"123\"";

  try

  User acc = objectMapper.readValuejson, User.class;

  System.out.printlnacc.getUid;

  System.out.printlnacc;

  catch JsonParseException e

  e.printStackTrace;

  catch JsonMappingException e

  e.printStackTrace;

  catch IOException e

  e.printStackTrace;

  /**

  * JSON转换为List对象

  */

  public static void readJson2List

  String json = "[\"uid\":1,\"uname\":\"www\",\"number\":234,\"upwd\":\"456\","

  + "\"uid\":5,\"uname\":\"tom\",\"number\":3.44,\"upwd\":\"123\"]";

  try

  List> list = objectMapper.readValue

  json, List.class;

  System.out.printlnlist.size;

  for int i = 0; i < list.size; i++

  Map map = list.geti;

  Set set = map.keySet;

  for Iterator it = set.iterator; it.hasNext;

  String key = it.next;

  System.out.printlnkey + ":" + map.getkey;

  catch JsonParseException e

  e.printStackTrace;

  catch JsonMappingException e

  e.printStackTrace;

  catch IOException e

  e.printStackTrace;

  /**

  * JSON转换为数组对象

  */

  public static void readJson2Array

  String json = "[\"uid\":1,\"uname\":\"www\",\"number\":234,\"upwd\":\"456\","

  + "\"uid\":2,\"uname\":\"sdfsdf\",\"number\":4745,\"upwd\":\"23456\"]";

  try

  User[] arr = objectMapper.readValuejson, User[].class;

  System.out.printlnarr.length;

  for int i = 0; i < arr.length; i++

  System.out.printlnarr[i];

  catch JsonParseException e

  e.printStackTrace;

  catch JsonMappingException e

  e.printStackTrace;

  catch IOException e

  e.printStackTrace;

  /**

  * JSON转换为Map对象

  */

  public static void readJson2Map

  String json = "\"success\":true,\"A\":\"address\": \"address2\",\"name\":\"haha2\",\"id\":2,\"email\":\"email2\","+

  "\"B\":\"address\":\"address\",\"name\":\"haha\",\"id\":1,\"email\":\"email\"";

  try

  Map> maps = objectMapper.readValuejson, Map.class;

  System.out.printlnmaps.size;

  Set key = maps.keySet;

  Iterator iter = key.iterator;

  while iter.hasNext

  String field = iter.next;

  System.out.printlnfield + ":" + maps.getfield;

  catch JsonParseException e

  e.printStackTrace;

  catch JsonMappingException e

  e.printStackTrace;

  catch IOException e

  e.printStackTrace;

  public static void mainString[] args

  user = new User;

  user.setUid5;

  user.setUname"tom";

  user.setUpwd"123";

  user.setNumber3.44;

  objectMapper = new ObjectMapper;

  try

  //jsonGenerator = objectMapper.getJsonFactory.createJsonGeneratorSystem.out, JsonEncoding.UTF8;

  // writeEntity2Json;

  // writeMap2Json;

  // writeList2Json;

  // writeOthersJSON;

  // readJson2Entity;

  // readJson2List;

  // readJson2Array;

  readJson2Map;

  catch Exception e

  e.printStackTrace;

  ========================User.java==============

  package com.bbs.utils.json;

  public class User

  private int uid;

  private String uname;

  private String upwd;

  private double number;

  public double getNumber

  return number;

  public void setNumberdouble number

  this.number = number;

  public int getUid

  return uid;

  public void setUidint uid

  this.uid = uid;

  public String getUname

  return uname;

  public void setUnameString uname

  this.uname = uname;

  public String getUpwd

  return upwd;

  public void setUpwdString upwd

  this.upwd = upwd;