JSONObject与GSON的一些常用的方法的使用

通过学习ajax然后接触了Json最后通过json接触到了JSONObject和Google的GSON,下面来一起看看JSONObject和GSON吧。

先附上依赖

//JSONObject依赖
 <dependency>
        <groupId>net.sf.json-lib</groupId>
        <artifactId>json-lib</artifactId>
        <version>2.4</version>
        <classifier>jdk15</classifier>
    </dependency>

顺便附上junit的依赖 毕竟用junit测试的

 <dependency>
        <groupId>junit</groupId>
        <artifactId>junit</artifactId>
        <version>4.10</version>
    </dependency>

差点忘记了这是FileUtils文件操作工具类的依赖

<dependency>
        <groupId>commons-io</groupId>
        <artifactId>commons-io</artifactId>
        <version>2.0.1</version>
    </dependency>

emmm直接上案例吧

package test;

import com.JSON.domain.User;
import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
import net.sf.json.JSONArray;
import net.sf.json.JSONObject;
import org.junit.Test;

import java.util.Date;
import java.util.HashMap;

public class JSONTEST {

    @Test
    public void TestJSON(){
        /**
         * 通过原生生成json数据格式
         */
        JSONObject jsonObject=new JSONObject();
        jsonObject.put("name","李四");
        jsonObject.put("home",true);
        jsonObject.put("number",134.11);
        jsonObject.put("sex","男");
        System.out.println(jsonObject.toString());
        /**
         *通过map来生成json数据格式
         */
        HashMap<String,Object> map=new HashMap<String, Object>();
        map.put("name","张三");
        map.put("home",true);
        map.put("技能",new String[]{"json","java","python"});
        System.out.println(JSONObject.fromObject(map).toString());
        JSONArray mapJson=JSONArray.fromObject(map);  //数组格式
        mapJson.add(1, new String[]{"dajhdka","dagdhag"});
//        mapJson.add(2,"是否有女朋友","");
        System.out.println(mapJson.toString());
        /**
         * 通过实体类生成json数据格式
         */
        User user=new User("小红","女",1550.2,false);
        JSONObject jsonObject1=JSONObject.fromObject(user);
        System.out.println(jsonObject1);
    }
    @Test
    public void TestGSON(){
        GsonBuilder gsonBuilder=new  GsonBuilder(); //设定格式的
        User user=new User("小红","女",1550.2,false,new Date());
        /**
         * String  toJson(object)  返回值是String 将对象转换成json格式
         */
        gsonBuilder.setDateFormat("yyyy-MM-dd HH:mm:ss"); //设定date属性 有误json本身没有date类型
        Gson gson=gsonBuilder.create();
        String Gjson=gson.toJson(user);
        System.out.println(Gjson);
        /**
         * 将JSON格式转换成实体类
         * 不清楚为什么我用了 复用功能的话 json格式转换成的实体类就没有值了 就是无法定位到
         */
        String content="{\"name\":\"小红\",\"sex\":\"女\",\"number\":1550.2,\"home\":false,\"birthday\":\"2020-02-17\"}";
        User user1=gson.fromJson(content,User.class);
        System.out.println(user1);


    }
}

JSONobject的案例
在这里插入图片描述

GSON的案例
在这里插入图片描述
实体类

package com.JSON.domain;

import com.google.gson.annotations.SerializedName;

import java.util.Date;

public class User {
    private String name;
//    @SerializedName("") //SEXGson提供了字段复用功能 :@SerializedName 大概就是别名的意思吧应该(不确定
    private String sex;
    private double number;
    private boolean home;
    private Date birthday;

    public Date getBirthday() {
        return birthday;
    }

    @Override
    public String toString() {
        return "User{" +
                "name='" + name + '\'' +
                ", sex='" + sex + '\'' +
                ", number=" + number +
                ", home=" + home +
                ", birthday=" + birthday +
                '}';
    }

    public void setBirthday(Date birthday) {
        this.birthday = birthday;
    }

    public  User(String name, String sex, double number, boolean home,Date birthday){
        this.name=name;
        this.sex=sex;
        this.number=number;
        this.home=home;
        this.birthday=birthday;
    }

    public  User(String name, String sex, double number, boolean home){
        this.name=name;
        this.sex=sex;
        this.number=number;
        this.home=home;

    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getSex() {
        return sex;
    }

    public void setSex(String sex) {
        this.sex = sex;
    }

    public double getNumber() {
        return number;
    }

    public void setNumber(double number) {
        this.number = number;
    }

    public boolean isHome() {
        return home;
    }

    public void setHome(boolean home) {
        this.home = home;
    }
}

上面都是一些基础的语法,下面这个案例是JSONObject读取外部json文件

首先是外部文件的位置 为什么放这里呢 因为.class的获取的路径就是这边的 所以我把JSON.json放这
在这里插入图片描述
JSON.json

{
  "name": "小红",
  "sex": "女",
  "number": 1667.2,
  "home": false

}
package test;

import net.sf.json.JSONObject;
import org.apache.commons.io.FileUtils;
import org.junit.Test;

import java.io.File;
import java.io.IOException;

public class JSONTextParsing {

    @Test
    public void JSONTest() throws IOException {
        //获取json文档
        /**
         * class.getResource("") 会获取target底下的class的 上一级的包
         *   这里是 file:/D:/IdeaProjects/Web/JSON_GSON__TEST/target/test-classes/test/
         */
        //        System.out.println(JSONTextParsing.class.getResource(""));
        File file=new File(JSONTextParsing.class.getResource("JSON.json").getFile());//找到该文件
        /**
         *  使用阿帕奇的jar包  commons-io
         *  获取文本信息 String类型 readFileToString 并保留格式
         */
        String content=FileUtils.readFileToString(file);
        JSONObject jsonObject=JSONObject.fromObject(content);
        System.out.println(jsonObject);


    }

}

在这里插入图片描述

其实还有很多方法没用到,emmm 反正到时候用到了再说吧… …

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值