/* * Copyright (c) 2019. 黄钰朝 * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ package com.hyc.wechat.test; import com.alibaba.fastjson.JSON; import com.alibaba.fastjson.JSONObject; import com.hyc.wechat.model.po.Message; import com.hyc.wechat.model.po.User; import java.util.Date; /** * @author 黄钰朝 * @description 该类用于测试JSON的解析和转换功能,将JSON字符串转换为User对象,并打印相关信息。 * @date 2019-05-04 01:02 */ public class TestJson { public static void main(String[] args) { // 解析JSON字符串为JSONObject对象 JSON json = JSONObject.parseObject( "{\"id\":\"2\", " + "\"wechat_id\":\"s\"}" // 注意:这里wechat_id应该是User类中定义的字段名 ); // 将JSONObject对象转换为User类的实例 User user = JSONObject.toJavaObject(json,User.class); // 打印User对象的id属性 System.out.println(user.getId()); // 打印User对象的wechat_id属性 System.out.println(user.getWechatId()); // 打印一个基于给定时间戳的日期字符串 System.out.println(new Date(1309237).toString()); // 打印User对象的字符串表示形式 System.out.println(user.toString()); } }