「java对象转json字符串」java将json字符串转换成对象
今天给各位分享java对象转json字符串的知识,其中也会对java将json字符串转换成对象进行解释,如果能碰巧解决你现在面临的问题,别忘了关注本站,现在开始吧!
本文目录一览:
- 1、java怎么把字符串转成json字符串
- 2、java ObjectMapper 将对象转换成json字符串问题
- 3、如何将java对象转为json字符串
- 4、java中字符串怎么转json?
- 5、如何便捷地将一个对象转化成为一个json字符串
java怎么把字符串转成json字符串
@RequestMapping(value = "updateInvestorApplyAccountNo", method = RequestMethod.POST)
@ResponseBody
public void updateInvestorApplyAccountNo(HttpServletRequest request,
HttpServletResponse response,
@RequestBody String requestBody) {
int num = 0;
String result = "";
//下面是把拿到的json字符串转成 json对象
JSONObject jsStr = JSONObject.parseObject(requestBody); //将字符串{“id”:1}
//int jsID = Integer.parseInt(jsStr.getString("id"));//获取id的值
/**
* json对象转换成java对象
*/
InvestorApplyModel stud = (InvestorApplyModel) JSONObject.toJavaObject(jsStr,InvestorApplyModel.class);
}
java ObjectMapper 将对象转换成json字符串问题
先给你一个正确的方法:
1,把bean里面的get方法上面的格式去掉
我的代码如下:
private Timestamp time;
public Timestamp getTime() {
return time;
}
public void setTime(Timestamp time) {
this.time = time;
}
测试方法:
public static void main(String[] args) throws JsonProcessingException, ParseException {
Timestamp timestamp = new Timestamp(System.currentTimeMillis());
AccountInfo accountInfo = new AccountInfo();
accountInfo.setTime(timestamp);
ObjectMapper mapper = new ObjectMapper();
mapper.setDateFormat(new SimpleDateFormat("yyyy-MM-dd hh:mm:ss"));
String s = mapper.writeValueAsString(accountInfo);
System.out.println(s);
long time = DateUtils.parseDate("1987-06-04 00:00:001","yyyy-MM-dd hh:mm:ss").getTime();
String date = DateUtils.parseDate("1987-06-04 00:00:01","yyyy-MM-dd hh:mm:ss").toString();
System.out.println(date);
timestamp = Timestamp.valueOf("1987-06-04 00:00:00");
System.out.println(timestamp);
accountInfo = new AccountInfo();
accountInfo.setTime(timestamp);
mapper = new ObjectMapper();
mapper.setDateFormat(new SimpleDateFormat("yyyy-MM-dd hh: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-16 01:27:16"}
Thu Jun 04 00:00:01 CDT 1987
1987-06-04 00:00:00.0
{"openId":null,"token":null,"ip":null,"account":null,"phone":null,"email":null,"platformType":0,"time":"1987-06-04 12: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小时计进即可。
如何将java对象转为json字符串
用Gson转换就行了,需要下载jar包
例子:
Person person=new Person();Gson gson=new Gson();String json=gson.toJson(person);
java中字符串怎么转json?
string类型如果要转换成json的话,就需要写成这样的形式,如下:\x0d\x0aString jsonStr ="{'id':'11','parentId':'root','refObj':{'existType':'exist','deptType':'emp','treeNodeType':'dept'}}";\x0d\x0a JSONObject jsonObj = new JSONObject(jsonStr);\x0d\x0a JSONObject refObj = new JSONObject(jsonObj.getString("refObj"));\x0d\x0a String existType = refObj.getString("existType");\x0d\x0a System.out.println(existType);\x0d\x0ajar使用的是org.json.jar
如何便捷地将一个对象转化成为一个json字符串
Gson(又称Google Gson)是Google公司发布的一个开放源代码的Java库,主要用途为序列化Java对象为JSON字符串,或反序列化JSON字符串成Java对象。
Gson的应用主要为toJson与fromJson两个转换函数,而在使用这种对象转换之前需先创建好对象的类以及其成员才能成功的将JSON字符串成功转换成相对应的对象。
class Examples {
private int answer1 = 100;
private String answer2 = "Hello world!";
Examples(){
} // default constructor
}
序列化JAVA对象成JSON字符串
Examples example1 = new Examples();
Gson gson = new Gson();
String json = gson.toJson(example1);
== json is {"answer1":100,"answer2":"Hello world!"}
java对象转json字符串的介绍就聊到这里吧,感谢你花时间阅读本站内容,更多关于java将json字符串转换成对象、java对象转json字符串的信息别忘了在本站进行查找喔。
发布于:2022-11-24,除非注明,否则均为
原创文章,转载请注明出处。