微信网站模版,已备案域名查询网,湘潭网站制作,郴州365网通过学习ajax然后接触了Json最后通过json接触到了JSONObject和Google的GSON#xff0c;下面来一起看看JSONObject和GSON吧。
先附上依赖
//JSONObject依赖dependencygroupIdnet.sf.json-lib/groupIdartifactIdjson-lib/artifactId下面来一起看看JSONObject和GSON吧。
先附上依赖
//JSONObject依赖dependencygroupIdnet.sf.json-lib/groupIdartifactIdjson-lib/artifactIdversion2.4/versionclassifierjdk15/classifier/dependency顺便附上junit的依赖 毕竟用junit测试的 dependencygroupIdjunit/groupIdartifactIdjunit/artifactIdversion4.10/version/dependency差点忘记了这是FileUtils文件操作工具类的依赖
dependencygroupIdcommons-io/groupIdartifactIdcommons-io/artifactIdversion2.0.1/version/dependencyemmm直接上案例吧
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 {Testpublic void TestJSON(){/*** 通过原生生成json数据格式*/JSONObject jsonObjectnew JSONObject();jsonObject.put(name,李四);jsonObject.put(home,true);jsonObject.put(number,134.11);jsonObject.put(sex,男);System.out.println(jsonObject.toString());/***通过map来生成json数据格式*/HashMapString,Object mapnew HashMapString, Object();map.put(name,张三);map.put(home,true);map.put(技能,new String[]{json,java,python});System.out.println(JSONObject.fromObject(map).toString());JSONArray mapJsonJSONArray.fromObject(map); //数组格式mapJson.add(1, new String[]{dajhdka,dagdhag});
// mapJson.add(2,是否有女朋友,);System.out.println(mapJson.toString());/*** 通过实体类生成json数据格式*/User usernew User(小红,女,1550.2,false);JSONObject jsonObject1JSONObject.fromObject(user);System.out.println(jsonObject1);}Testpublic void TestGSON(){GsonBuilder gsonBuildernew GsonBuilder(); //设定格式的User usernew User(小红,女,1550.2,false,new Date());/*** String toJson(object) 返回值是String 将对象转换成json格式*/gsonBuilder.setDateFormat(yyyy-MM-dd HH:mm:ss); //设定date属性 有误json本身没有date类型Gson gsongsonBuilder.create();String Gjsongson.toJson(user);System.out.println(Gjson);/*** 将JSON格式转换成实体类* 不清楚为什么我用了 复用功能的话 json格式转换成的实体类就没有值了 就是无法定位到*/String content{\name\:\小红\,\sex\:\女\,\number\:1550.2,\home\:false,\birthday\:\2020-02-17\};User user1gson.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;}Overridepublic 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.namename;this.sexsex;this.numbernumber;this.homehome;this.birthdaybirthday;}public User(String name, String sex, double number, boolean home){this.namename;this.sexsex;this.numbernumber;this.homehome;}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 {Testpublic 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 filenew File(JSONTextParsing.class.getResource(JSON.json).getFile());//找到该文件/*** 使用阿帕奇的jar包 commons-io* 获取文本信息 String类型 readFileToString 并保留格式*/String contentFileUtils.readFileToString(file);JSONObject jsonObjectJSONObject.fromObject(content);System.out.println(jsonObject);}}其实还有很多方法没用到emmm 反正到时候用到了再说吧… …