日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 前端技术 > javascript >内容正文

javascript

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

發布時間:2025/3/11 javascript 17 豆豆
生活随笔 收集整理的這篇文章主要介紹了 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 {@Testpublic 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);}@Testpublic 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;}@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.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 {@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 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 反正到時候用到了再說吧… …

總結

以上是生活随笔為你收集整理的JSONObject与GSON的一些常用的方法的使用的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。