❶ 如何使用ObjectMapper的方式實現json和bean的自由轉換
網上很流行的一種json和bean之間轉換的方式是 通過宏物JSONObject和JSONArray來讀辯絕清取json中的數據,然後新建相應對象並放入其中。也就是手動擋創建對象。 這種方法不錯,但是最近發現還有一種方法就是用 ObjectMapper.ReadValue的方式攜前。
❷ 如何使用objectmapper的方式實現json和map的自由轉換
第一種:
java code
public class JsonToJavaBean {
public static void main(String[] args) {
String str="{\"student\":[{\"name\":\"leilei\",\"age\":23},{\"name\":\"leilei02\",\"age\":23}]}";
Student stu = null;
List<Student> list = null;
try {
ObjectMapper objectMapper=new ObjectMapper();
StudentList studentList=objectMapper.readValue(str, StudentList.class);
list=studentList.getStudent();
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
for(Student s:list){
System.out.println(s.getName()+" "+s.getAge());
}
}
}
第二種:
Java code
public static void main(String[] args) {
ArrayList<Student> list=new ArrayList<Student>();
Student s1=new Student();
s1.setName("leilei");
s1.setAge(23);
Student s2=new Student();
s2.setName("leilei02");
s2.setAge(23);
list.add(s1);
list.add(s2);
StringWriter str=new StringWriter();
ObjectMapper objectMapper=new ObjectMapper();
try {
objectMapper.writeValue(str, list);
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
System.out.println(str);
}
前幾天剛遇到這個問題,就順便把json完整的學了一遍。
希望能符合您的要求。
❸ 如何將json轉成對應的model
大部分情況使用json的格式來傳遞參數,但是使用起來就不太方便了,因此需要將json轉換成對應的model,然後對model進行操作就很便利了。json轉model的方法也有很多,推薦一個就是,jackson,方法是這樣的。
public static <T>T obj2Obj(Object fromValue, Class<T> toValueType){
ObjectMapper mapper = new ObjectMapper();
mapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);
T t = mapper.convertValue(fromValue, toValueType);
return t;
}
❹ java怎麼將json轉換成對象
可以使用jackson的 new ObjectMapper().readValue(String, bean.class);
或者使用 JSONObject.toBean(String,bean.class);
都可以實現json轉化為bean
而 jackson 或者 JSONObject 的jar包網上均有下載,把下載的jar導入你的項目中即可。
❺ 如何使用ObjectMapper的方式實現Json和bean的自由轉換
public class JsonToJavaBean {
public static void main(String[] args) {
String str="{\"student\":[{\"name\"裂和:\"leilei\",\"age\":23},{\"name\":\"leilei02\",\"age\"游差:23}]}";
Student stu = null;
List<Student> list = null;
try {
ObjectMapper objectMapper=new ObjectMapper();
StudentList studentList=objectMapper.readValue(str, StudentList.class);
list=studentList.getStudent();
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
for(Student s:list){
System.out.println(s.getName()+"肆磨盯 "+s.getAge());
}
}
}
❻ java ObjectMapper 將對象轉換成json字元串問題
先給你一個正確的方法:
1,把bean裡面的get方法上面的格式去掉
我的代碼如下:
privateTimestamptime;
publicTimestampgetTime(){
returntime;
}
publicvoidsetTime(Timestamptime){
this.time=time;
}
測試方法:
publicstaticvoidmain(String[]args)throwsJsonProcessingException,ParseException{
Timestamptimestamp=newTimestamp(System.currentTimeMillis());
AccountInfoaccountInfo=newAccountInfo();
accountInfo.setTime(timestamp);
ObjectMappermapper=newObjectMapper();
mapper.setDateFormat(newSimpleDateFormat("yyyy-MM-ddhh:mm:ss"));
Strings=mapper.writeValueAsString(accountInfo);
System.out.println(s);
longtime=DateUtils.parseDate("1987-06-0400:00:001","yyyy-MM-ddhh:mm:ss").getTime();
Stringdate=DateUtils.parseDate("1987-06-0400:00:01","yyyy-MM-ddhh:mm:ss").toString();
System.out.println(date);
timestamp=Timestamp.valueOf("1987-06-0400:00:00");
System.out.println(timestamp);
accountInfo=newAccountInfo();
accountInfo.setTime(timestamp);
mapper=newObjectMapper();
mapper.setDateFormat(newSimpleDateFormat("yyyy-MM-ddhh:mm:ss"));
s=mapper.writeValueAsString(accountInfo);
System.out.println(s);
}
輸出結果:
{"openId":null,"token":null,"ip":null,"account":null,"phone":null,"email":null,"platformType":0,"time":"2018-10-1601:27:16"}
ThuJun0400:00:01CDT1987
1987-06-0400:00:00.0
{"openId":null,"token":null,"ip":null,"account":null,"phone":null,"email":null,"platformType":0,"time":"1987-06-0412:00:00"}
不過這里有一個小問題,因為你是使用的yyyy-MM-dd hh:mm:ss 格式,而hh表示按12小時計時,所以1987-06-04 00:00:00會在json中表示為1987-06-04 12:00:00,你可以換成yyyy-MM-dd HH:mm:ss按24小時計進即可。
❼ 如何使用ObjectMapper的方式實現Json和bean的自由轉換
使用ObjectMapper的方式實現Json和bean的自由轉換:
第一種:Java code
publicclassJsonToJavaBean{
publicstaticvoidmain(String[]args){
Stringstr="{"student":[{"name":"leilei","age":23},{"name":"leilei02","age":23}]}";
Studentstu=null;
List<Student>list=null;
try{
ObjectMapperobjectMapper=newObjectMapper();
StudentListstudentList=objectMapper.readValue(str,StudentList.class);
褲行list=studentList.getStudent();
}catch(Exceptione){
//TODOAuto-generatedcatchblock
e.printStackTrace();
}
for(Students:list){
System.out.println(s.getName()+""+s.getAge());
}
}
}
第二種:Java code
publicstaticvoidmain(String[]args){
ArrayList<Student>list=newArrayList<Student>();
Students1=newStudent();
s1.setName("leilei");
s1.setAge(23);
Students2=newStudent();
s2.setName("leilei02");
s2.setAge(23);
list.add(s1);
list.add(s2);
攜鏈StringWriterstr=newStringWriter();
辯純孫
ObjectMapperobjectMapper=newObjectMapper();
try{
objectMapper.writeValue(str,list);
}catch(Exceptione){
//TODOAuto-generatedcatchblock
e.printStackTrace();
}
System.out.println(str);
}
❽ 如何使用ObjectMapper的方式實現Json和bean的自由轉換
ObjectMapper是jackson中的方法。
第一、將json字元串轉為bean
public class
StudentList { List student; public List getStudent() { return
student; } public void setStudent(List student) { this.student =
student; } @Override public String toString() { return "StudentList
[student=" + student + "]"; } }
public class JsonToJavaBean {
public static void main(String[] args) {
String str="{\"student\":[{\"name\":\"leilei\",\"age\":23},{\"笑宏name\":\"leilei02\",\"age\":23}]}";
Student stu = null;
List list = null;
try {
ObjectMapper objectMapper=new ObjectMapper();
StudentList studentList=objectMapper.readValue(str, StudentList.class);
list=studentList.getStudent();
} catch (Exception e) {
//滾升枝 TODO Auto-generated catch block
e.printStackTrace();
}
for(Student s:list){
System.out.println(s.getName()+" "+s.getAge());
}
}
}
第二、將bean轉為json
public static void main(String[] args) {
ArrayList list=new ArrayList();
Student s1=new Student();
s1.setName("leilei");
s1.setAge(23);
Student s2=new Student();
s2.setName("大敏leilei02");
s2.setAge(23);
list.add(s1);
list.add(s2);
StringWriter str=new StringWriter();
ObjectMapper objectMapper=new ObjectMapper();
try {
objectMapper.writeValue(str, list);
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
System.out.println(str);
}
❾ 在java中怎麼將json轉換成對應的類型
可以使用jackson的 new ObjectMapper().readValue(String, bean.class); 或者使用 JSONObject.toBean(String,bean.class); 都可以實現json轉化為bean 而 jackson 或者 JSONObject 的jar包網上均有下載,把下載的jar導入你的項目中即可。
❿ JSON對象與字元串相互轉化ObjectMapper
JSON(JavaScript Object Notation) 是一種輕量級的數據交換孫銀格式。 易於人閱讀和編寫。同時也易於機器解析和則告宴生成。
對象是一個無序的「『名稱/值』對」集合。一個對象以「{」(左括弧)開始,「}」(右括弧)結束。每個「名稱」後跟一個「:」(冒號);「『名稱/值』 對」之間使用「,」(逗號)分隔。
數組是值(友念value)的有序集合。一個數組以「[」(左中括弧)開始,「]」(右中括弧)結束。值之間使用「,」(逗號)分隔。