导航:首页 > 编程语言 > objectmapperjson

objectmapperjson

发布时间:2021-02-27 14:47:13

1. 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小时计进即可。

2. 如何使用ObjectMapper的方式实现Json和bean的自由转换

第一、将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);
}

3. java中从服务器上获取的json字符串,怎么存map<integer object>中

如果使用的

net.sf.json

包,可以先用 JSONObject.fromJson(String)转成jsonobject对象,然后使用toBean方法转成map,或者用get去一个个版的取。
如果用权的jackson包,
private static ObjectMapper mapper = new ObjectMapper();
mapper.readValue(json, Map.class);

4. objectmapper 有类似于jsonobject.put一样的api吗

:可抄能要引几个包; JSONObject jsonObject = new JSONObject(); jsonObject.put("ret", 0); jsonObject.put("msg", "ok"); jsonObject.put("date", "data"); System.out.println(jsonObject.toString());//你可以打印看一下

5. 如何使用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完整的学了一遍。
希望能符合您的要求。

6. 如何使用ObjectMapper的方式实现Json和bean的自由转换

第一种:

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完整的学了一遍。

7. 如何使用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());
}
}
}

8. 如何使用ObjectMapper的方式实现Json和bean的自由转换

网上很流行的一种json和bean之间转换的方式是 通过JSONObject和JSONArray来读取json中的数据,然后新建相内应对象并放入其中。也就是容手动挡创建对象。 这种方法不错,但是最近发现还有一种方法就是用 ObjectMapper.ReadValue的方式。

9. objectmapper 和 jsonparser有什么区别

区别在于JSONObject是一个{}包裹起来的一个对象(Object), 而JSONArray则是[]包裹起来的一个数组(Array), 说白点就是一个是数组一个是对象或字符串

阅读全文

与objectmapperjson相关的资料

热点内容
紫轩小说吧txt精选珍藏 浏览:745
js中循环li元素 浏览:824
有关美国农场的电影 浏览:509
win10gdi32lib 浏览:140
菲律宾片《桨叶》 浏览:234
小电影网站推荐 浏览:619
如何从数据分析获利并产生影响力 浏览:686
点付款码出现网络不可用什么情况 浏览:659
米璐璐作品集下载txt 浏览:238
cad新建文件怎么不显示 浏览:380
重生香港1973炒股 浏览:400
日本大奶电影 浏览:490
linux空间不足如何解决办法 浏览:205
网络订单处理 浏览:589
团鬼六所有的电影 浏览:157
十大必看火影小说排行榜 浏览:580
大香蕉一区 浏览:697
台版iphone6有什么区别 浏览:711
小说电影图片 浏览:114
求个在线看h的网站 浏览:859

友情链接