JSON与xml简介

1,Json是一种轻量级的数据交换格式,易于阅读和编写同时也易于解析和传输。
2,Json应用场景
   RPC远程调用时,和提供供给外部访问接口,前端ajax异步访问数据时都用到了Json。
3,封装Json
流行框架fastjson  gson  jackson
使用fastjson 解析JSON
1在pom.xml中添加依赖
   <!-- 添加阿里巴巴解析json工具类 -->
        <dependency>
         <groupId>com.alibaba</groupId>
         <artifactId>fastjson</artifactId>
          <version>1.1.26</version>
       </dependency>
2,代码:
public static void fastJson(){
JSONObject root = new JSONObject();
root.put("error", 0);
root.put("message", "接口调用成功");
JSONObject user = new JSONObject();
user.put("name", "王池");
user.put("sex", 1);
JSONArray Userwangchi = new JSONArray();
Userwangchi.add(user);
root.put("date", user);
System.out.println(root.toJSONString());
}

猜你喜欢

转载自blog.csdn.net/wjc2013481273/article/details/80813650