-
JSON會(huì)把所有生成的空格去掉。這個(gè)要嘗試一下。查看全部
-
JAVA的標(biāo)準(zhǔn)json包,new JSONObject,put方法。 put(key,value) value:null,true,false,Number,String 在put一個(gè)null的時(shí)候先new一個(gè)null的Object 大括號為對象,中括號為數(shù)組查看全部
-
常見的json 實(shí)例查看全部
-
總結(jié) (1)JSON Android SDK官方的庫 (2)GSON 適用于服務(wù)端的開發(fā) (3)Gson 比 JSON 強(qiáng)大 JSON庫總結(jié): 功能:映射Java Object與json格式數(shù)據(jù) 1.通過Annotation注解來聲明 2.支持自定義屬性名稱 3.支持包含或排除屬性 4.支持自定義接口自己完成解析/生成過程查看全部
-
Gson可以將json文件中的其他類型的數(shù)據(jù),比如日期、數(shù)組等直接轉(zhuǎn)換成java屬性的集合類對象 Gson的優(yōu)點(diǎn): (1)支持日期 (2)可以轉(zhuǎn)換為對象 (3)集合操作 eg: private List<String> major; private Set<String> major; System.out.println(wangxiaoer.getMajor()); System.out.println(wangxiaoer.getMajor().getClass());查看全部
-
Gson 帶有日期轉(zhuǎn)化的功能 Gson gson = new GsonBuilder().setDateFormat("yyyy-MM-dd").create(); DiaosiWithBirthday wangxiaoer = gson.from(content,DiaosiWithBirthday.class);//DiaosiWithBirthday的屬性是java.util.Date的類型查看全部
-
Gson: 在java獲取json數(shù)據(jù),首先把json文件實(shí)例化為file對象,然后用file的工具類的靜態(tài)方法轉(zhuǎn)換成字符串,如FileUtils.readFileToString(file),然后實(shí)例化Gson對象,再調(diào)用fromJson方法解析json。 JSONObject可以將*.json文件解析成一個(gè)它的對象;Gson可以正向的生成,也可以解析成自己定義的一個(gè)JavaBean的對象 eg:package gson; import java.io.File; import java.io.IOException; import org.apache.commons.io.FileUtils; import com.google.gson.Gson; import bean.Diaosi; import json.ReadJSONSample; public class GsonReadSample { public static void main(String[] args) throws IOException { // TODO 自動(dòng)生成的方法存根 File file=new File(ReadJSONSample.class.getResource("/wangxiaoer.json").getFile()); String content=FileUtils.readFileToString(file); Gson gson=new Gson(); Diaosi wangxiaoer=gson.fromJson(content, Diaosi.class); System.out.println(wangxiaoer); } }查看全部
-
Gson 可以使用注解靈活改變列名(構(gòu)造函數(shù)) Gson 可以使用transient隱藏不顯示列名:javaBean中列屬性private后面添加 transiant eg:private transient String ignore; Gson 可以使用GsonBuilder進(jìn)行json美化工作 eg:GsonBuilder gsonBuilder=new GsonBuilder(); gsonBuilder.setPrettyPrinting(); Gson 可以使用setFieldNamingStrategy(new FieldNamingStrategy){ 使用回調(diào)函數(shù),設(shè)置json輸出格式} eg: gsonBuilder.setFieldNamingStrategy(new FieldNamingStrategy() { public String translateName(Field f) { // TODO 自動(dòng)生成的方法存根 if(f.getName().equals("name")) return "Name"; return f.getName(); } }); Gson gson=gsonBuilder.create(); System.out.println(gson.toJson(wangxiaoer));查看全部
-
在java中使用com.google.code.gson包創(chuàng)建json,直接實(shí)例化Gson對象,然后使用Gson對象的toJson方法創(chuàng)建json,如Gson gson=new Gson();gson.toJson(javabean); eg:Gson gson=new Gson(); System.out.println(gson.toJson(wangxiaoer));查看全部
-
Gson依賴 <dependency> <groupId>com.google.code.gson</groupId> <artifactId>gson</artifactId> <version>2.4</version> </dependency>查看全部
-
Gson 可以用注解的方式實(shí)現(xiàn) 列名轉(zhuǎn)化:@SerializedName("key的字符串"); eg: public class Diaosi { @SerializedName("Name") private String name; @SerializedName("School") private String school; @SerializedName("Has_girlfriend") private boolean has_girlfriend; @SerializedName("Age") private double age; @SerializedName("Car") private Object car; @SerializedName("House") private Object house; @SerializedName("Major") private String[] major; @SerializedName("Comment") private String comment; @SerializedName("Birthday") private String birthday; }查看全部
-
Gson 是 Google 提供的用來在 Java 對象和 JSON 數(shù)據(jù)之間進(jìn)行映射的 Java 類庫。可以將一個(gè) JSON 字符串轉(zhuǎn)成一個(gè) Java 對象,或者反過來。查看全部
-
判斷Json數(shù)據(jù)中是否有指定的信息 JSONObject jsonObject = new JSONObject(content); if (!jsonObject.isNull("name")) System.out.println("姓名:" + jsonObject.getString("name")); if (!jsonObject.isNull("age")) System.out.println("年齡:" + jsonObject.getDouble("age")); if (!jsonObject.isNull("birthday")) System.out.println("生日:" + jsonObject.getString("birthday")); if (!jsonObject.isNull("school")) System.out.println("學(xué)校:" + jsonObject.getString("school"));查看全部
-
package json; import java.io.File; import java.io.IOException; import org.apache.commons.io.FileUtils; import org.json.JSONArray; import org.json.JSONException; import org.json.JSONObject; public class ReadJSONSample { public static void main(String[] args) throws IOException, JSONException { // TODO 自動(dòng)生成的方法存根 File file=new File(ReadJSONSample.class.getResource("/wangxiaoer.json").getFile()); String content=FileUtils.readFileToString(file); JSONObject jsonObject=new JSONObject(content); System.out.println("姓名:"+jsonObject.getString("name")); System.out.println("年齡:"+jsonObject.getDouble("age")); System.out.println("生日:"+jsonObject.getString("birthday")); System.out.println("學(xué)校:"+jsonObject.getString("school")); JSONArray majorArray=jsonObject.getJSONArray("major"); for (int i = 0; i < majorArray.length(); i++) { String m=(String) majorArray.get(i); System.out.println("專業(yè)-"+(i+1)+":"+m); } } }查看全部
-
把文件轉(zhuǎn)換成JASONOBJECT 1.獲取文件,通過獲取文件地址:.class.getResource(); 2.獲取文件內(nèi)容FileUtiles.readFileTOString(); 3.轉(zhuǎn)換成JasonObject格式,通過getString()獲取key值 maven引FileUtils相關(guān)的jar包依賴。 <dependency> <groupId>commons-io</groupId> <artifactId>commons-io</artifactId> <version>2.4</version> </dependency>查看全部
舉報(bào)
0/150
提交
取消