本文简单小结下 jackson的常用操作 ,使用的是1.9.13的版本;
 
1) 简单pojo转换json:
     
转换:
    
如果要转换为良好的格式,则:
    
json转换为pojo:
      
2 接下来看如何把hashmap转换为json:
    
再把hashmap反转为json
     
已有 0人发表留言,猛击->> 这里<<-参与讨论
ITeye推荐
                       
                           
                       
                     1) 简单pojo转换json:
public class Employee
{
   private Integer id;
   private String firstName;
   private String lastName;
   public Employee(){
   }
   public Employee(Integer id, String firstName, String lastName, Date birthDate){
      this.id = id;
      this.firstName = firstName;
      this.lastName = lastName;
   }
   public Integer getId()
   {
      return id;
   }
   public void setId(Integer id)
   {
      this.id = id;
   }
   public String getFirstName()
   {
      return firstName;
   }
   public void setFirstName(String firstName)
   {
      this.firstName = firstName;
   }
   public String getLastName()
   {
      return lastName;
   }
   public void setLastName(String lastName)
   {
      this.lastName = lastName;
   }
   @Override
   public String toString()
   {
      return "Employee [id=" + id + ", firstName=" + firstName + ", " +"lastName=" + lastName + "]";
   }
}转换:
public class JavaToJSONExample
{
   public static void main(String[] args)
   {
      @SuppressWarnings("deprecation")
      Employee employee = new Employee(1, "Lokesh", "Gupta", new Date(1981,8,18));
      ObjectMapper mapper = new ObjectMapper();
      try
      {
         mapper.writeValue(new File("c://temp/employee.json"), employee);
      } catch (JsonGenerationException e)
      {
         e.printStackTrace();
      } catch (JsonMappingException e)
      {
         e.printStackTrace();
      } catch (IOException e)
      {
         e.printStackTrace();
      }
   }
}
Output:
//In employee.json file below content will be written
{"id":1,"firstName":"Lokesh","lastName":"Gupta"}如果要转换为良好的格式,则:
public class JavaToPrettyJSONExample
{
   public static void main(String[] args)
   {
      @SuppressWarnings("deprecation")
      Employee employee = new Employee(1, "Lokesh", "Gupta", new Date(1981,8,18));
      ObjectMapper mapper = new ObjectMapper();
      try
      {
         mapper.defaultPrettyPrintingWriter().writeValue(new File("c://temp/employee.json"), employee);
      } catch (JsonGenerationException e)
      {
         e.printStackTrace();
      } catch (JsonMappingException e)
      {
         e.printStackTrace();
      } catch (IOException e)
      {
         e.printStackTrace();
      }
   }
}
Output:
{"id" : 1,"firstName" : "Lokesh","lastName" : "Gupta"
}json转换为pojo:
public class JSONToJavaExample
{
   public static void main(String[] args)
   {
      Employee employee = null;
      ObjectMapper mapper = new ObjectMapper();
      try
      {
         employee =  mapper.readValue(new File("c://temp/employee.json"), Employee.class);
      } catch (JsonGenerationException e)
      {
         e.printStackTrace();
      } catch (JsonMappingException e)
      {
         e.printStackTrace();
      } catch (IOException e)
      {
         e.printStackTrace();
      }
      System.out.println(employee);
   }
}
Output:
Employee [id=1, firstName=Lokesh, lastName=Gupta]2 接下来看如何把hashmap转换为json:
public class JavaToHashMapExample
{
   public static void main(String[] args)
   {
      HashMap<string, object=""> hashmap = new HashMap<string, object="">();
      hashmap.put("id", 11);
      hashmap.put("firstName", "Lokesh");
      hashmap.put("lastName", "Gupta");
      ObjectMapper mapper = new ObjectMapper();
      try
      {
         mapper.writeValue(new File("c://temp/data.json"), hashmap);
      } catch (JsonGenerationException e)
      {
         e.printStackTrace();
      } catch (JsonMappingException e)
      {
         e.printStackTrace();
      } catch (IOException e)
      {
         e.printStackTrace();
      }
   }
}
File content:
{"id":11,"lastName":"Gupta","firstName":"Lokesh"}再把hashmap反转为json
public class HashMapToJSONExample
{
   public static void main(String[] args)
   {
      HashMap<string, object=""> hashmap = new HashMap<string, object="">();
      ObjectMapper mapper = new ObjectMapper();
      try
      {
         hashmap = mapper.readValue(new File("c://temp/data.json"), 
               new TypeReference<map<string, object="">>(){
                                                       });
      } catch (JsonGenerationException e)
      {
         e.printStackTrace();
      } catch (JsonMappingException e)
      {
         e.printStackTrace();
      } catch (IOException e)
      {
         e.printStackTrace();
      }
      System.out.println(hashmap);
   }
}
Output:
{id=11, lastName=Gupta, firstName=Lokesh}已有 0人发表留言,猛击->> 这里<<-参与讨论
ITeye推荐
