json是什么,看了之后我才恍然大悟(二)

简介: 3. 返回一个List集合4. 返回一个时间(重要)5. FastJson

3. 返回一个List集合

@RequestMapping("/j2")
@ResponseBody 
private String jso2() throws JsonProcessingException {
    ObjectMapper mapper = new ObjectMapper();
    ArrayList<User> userList = new ArrayList<User>();
    User user1 = new User("王木木1", 18, "男");
    User user2 = new User("王木木2", 18, "男");
    User user3 = new User("王木木3", 18, "男");
    User user4 = new User("王木木4", 18, "男");
    userList.add(user1);
    userList.add(user2);
    userList.add(user3);
    userList.add(user4);
    String str = mapper.writeValueAsString(userList);
    return str;
}

他会通过一个[]把我们所有的对象包裹起来,是一个集合


微信图片_20211230222840.png


4. 返回一个时间


@RequestMapping("/j3")
@ResponseBody
private String jso3() throws JsonProcessingException {
    ObjectMapper mapper = new ObjectMapper();
    Date date = new Date();
    //ObjectMapper,时间解析后的默认格式为Timestamp,时间戳
    return mapper.writeValueAsString(date);
}

上述出来是一串数字,那么如果我们想得到更理想的时间,可以使用一下集中方法格式化


4.1 自定义日期

@RequestMapping("/j3")
@ResponseBody
private String jso3() throws JsonProcessingException {
    ObjectMapper mapper = new ObjectMapper();
    Date date = new Date();
    //自定义日期的格式
    SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH-mm-ss");
    simpleDateFormat.format(date);
    //ObjectMapper,时间解析后的默认格式为Timestamp,时间戳
    return mapper.writeValueAsString(simpleDateFormat);
}

4.2 使用ObjectMapper 来改掉默认格式输出

@RequestMapping("/j4")
@ResponseBody
private String json4() throws JsonProcessingException {
    ObjectMapper mapper = new ObjectMapper();
    //不适用时间戳的方式
    mapper.configure(SerializationFeature.WRITE_DATES_AS_TIMESTAMPS,false);
    //自定义日期的格式
    SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH-mm-ss");
    mapper.setDateFormat(simpleDateFormat);
    Date date = new Date();
    //ObjectMapper,时间解析后的默认格式为Timestamp,时间戳
    return mapper.writeValueAsString(date);
}

(重要)


上面的方式我们就可以打包称一个工具类。


public class JsonUtils {
    public static String getJson(Object object,String simpleDateFormat){
        ObjectMapper mapper = new ObjectMapper();
        //不适用时间戳的方式
        mapper.configure(SerializationFeature.WRITE_DATES_AS_TIMESTAMPS,false);
        //自定义日期的格式
        SimpleDateFormat sdf = new SimpleDateFormat(simpleDateFormat);
        mapper.setDateFormat(sdf);
        try {
            return mapper.writeValueAsString(object);
        } catch (JsonProcessingException e) {
            e.printStackTrace();
        }
        return null;
    }
}


我们的方法就变得简单了:

@RequestMapping("/j5")
@ResponseBody
private String json5() throws JsonProcessingException {
    Date date = new Date();
    return JsonUtils.getJson(date,"yyyy-MM-dd HH-mm-ss");
}

微信图片_20211230223020.png


我们重载上面的方法,我们该怎么进行呢?


源码的思想

public static String getJson(Object object){
    return getJson(object,"yyyy-MM-dd HH-mm-ss");
}

直接调用上面的方法即可,源码的好多方法都是这么来进行的。


后端转成json要注意几个点:


  • 要导入jackson-databind的包
  • 配置乱码问题
  • 返回json要么用@Controller+@RequestMapping,要么使用@RestController
  • 也可以构建个工具


5. FastJson

<!-- https://mvnrepository.com/artifact/com.alibaba/fastjson -->
<dependency>
    <groupId>com.alibaba</groupId>
    <artifactId>fastjson</artifactId>
    <version>1.2.78</version>
</dependency>


主要的三个类:


  • JSONObject代表json对象


  • JSONObject实现了Map接口,猜想JSONObject底层操作是由Map实现的。
  • JSONObject对应json对象,通过各种形式的get()方法可以获取json对象中的数据,也可利用size()。isEmpty()等方法获取“键:值”对的个数和判断是否为空,其本质是通过实现Map接口并调用接口中的方法完成的。


  • JSONArray代表json对象数组


  • 内部具有List接口中的方法来完成操作


  • JSON代表JSONObject和JSONArray的转化


  • JSON类源码分析与使用


  • 观察这些方法,主要是实现json对象,json对象数组,javabean对象,jsoon字符串之间的相互转化。
@RequestMapping("/j6")
@ResponseBody
private String json6(){
    ArrayList<User> userList = new ArrayList<User>();
    User user1 = new User("王木木1", 18, "男");
    User user2 = new User("王木木2", 18, "男");
    User user3 = new User("王木木3", 18, "男");
    User user4 = new User("王木木4", 18, "男");
    userList.add(user1);
    userList.add(user2);
    userList.add(user3);
    userList.add(user4);
    //java对象转json字符串
    String str = JSON.toJSONString(userList);
    String str2 = JSON.toJSONString(user1);
    System.out.println(str);
    //json字符串转java对象
    User u1 = JSON.parseObject(str2,User.class);
    System.out.println(u1);
    //java对象转json字对象
    JSONObject jo1 = (JSONObject)JSON.toJSON(user2);
    System.out.println(jo1);
    //json对象转java对象
    User to_java_user = JSON.toJavaObject(jo1,User.class);
    System.out.println(to_java_user);
    return "test";
}

微信图片_20211230231916.png

相关文章
|
存储 JSON JavaScript
json是什么,看了之后我才恍然大悟(一)
十、JSON 前言 1.Controller返回json数据 2.上面的乱码问题
json是什么,看了之后我才恍然大悟(一)
|
存储 JSON JavaScript
JSON 是什么
JSON 是什么
319 0
|
XML JSON 前端开发
JSON是什么,为什么这么流行?
前几天分享了《Spring Boot 返回 JSON 数据,一分钟搞定!》,好些人对 JSON 还没有一个清晰的认识,今天栈长带大家来认识一下什么是JSON。
|
2月前
|
JSON API 数据格式
淘宝拍立淘按图搜索API系列,json数据返回
淘宝拍立淘按图搜索API系列通过图像识别技术实现商品搜索功能,调用后返回的JSON数据包含商品标题、图片链接、价格、销量、相似度评分等核心字段,支持分页和详细商品信息展示。以下是该API接口返回的JSON数据示例及详细解析:
|
2月前
|
JSON 算法 API
Python采集淘宝商品评论API接口及JSON数据返回全程指南
Python采集淘宝商品评论API接口及JSON数据返回全程指南
|
3月前
|
机器学习/深度学习 JSON 监控
淘宝拍立淘按图搜索与商品详情API的JSON数据返回详解
通过调用taobao.item.get接口,获取商品标题、价格、销量、SKU、图片、属性、促销信息等全量数据。
|
2月前
|
JSON API 数据安全/隐私保护
Python采集淘宝拍立淘按图搜索API接口及JSON数据返回全流程指南
通过以上流程,可实现淘宝拍立淘按图搜索的完整调用链路,并获取结构化的JSON商品数据,支撑电商比价、智能推荐等业务场景。
|
3月前
|
JSON 缓存 自然语言处理
多语言实时数据微店商品详情API:技术实现与JSON数据解析指南
通过以上技术实现与解析指南,开发者可高效构建支持多语言的实时商品详情系统,满足全球化电商场景需求。
|
3月前
|
JSON API 数据格式
干货满满!淘宝商品详情数据,淘宝API(json数据返回)
淘宝商品详情 API 接口(如 taobao.item.get)的 JSON 数据返回示例如下

热门文章

最新文章