导航:首页 > 编程语言 > 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相关的资料

热点内容
微信小程序按钮颜色 浏览:69
长江大学网课用什么app 浏览:431
华中系统图纸编程哪个刀好 浏览:38
地方债务数据在哪里查看 浏览:932
扫描文件怎么设置格式 浏览:957
苹果邮箱主机名填什么 浏览:630
多张图片同一个文件夹 浏览:798
win7怎么打开shs文件 浏览:481
怎么把文件夹做成iso 浏览:164
缤客网站上的房价怎么在哪里修改 浏览:406
单片机c51计数器实验代码 浏览:990
宏编程鼠标代表什么意思 浏览:753
别人捡到苹果6有用吗 浏览:829
word文件用wps打开 浏览:477
macbook修改文件格式软件 浏览:757
美版s7edge那个版本好 浏览:529
视频隐藏在文件夹里 浏览:144
网络通讯基础是什么 浏览:209
办公电脑文件管理 浏览:222
火化费报销文件有哪些 浏览:998

友情链接