-
package bean; public class Diaosi { private String name; private String school; private boolean has_girlfriend; private double age; private Object car; private Object house; private String[] major; private String comment; private String birthday; }查看全部
-
private static void createJsonByBean() { Diaosi wangxiaoer = new Diaosi(); wangxiaoer.setName("王小二"); wangxiaoer.setAge(25.2); wangxiaoer.setBirthday("1990-01-01"); wangxiaoer.setSchool("藍(lán)翔"); wangxiaoer.setMajor(new String[] { "理發(fā)", "挖掘機(jī)" }); wangxiaoer.setHas_girlfriend(false); wangxiaoer.setCar(null); wangxiaoer.setHouse(null); wangxiaoer.setComment("這是一個(gè)注釋"); System.out.println(new JSONObject(wangxiaoer)); }查看全部
-
有3種方式,創(chuàng)建json對(duì)象, 1、使用JSONObject對(duì)象put方法來構(gòu)建; 2、使用HashMap及其子類如treeMap的put方法來構(gòu)建; 3、使用JavaBean來構(gòu)建JSONObject對(duì)象。 出現(xiàn)"major":[{"bytes":[{},{},{},{},{},{}],"empty":false}的可以換個(gè)org.json的新版本,親測(cè)可用。 Maven依賴: <dependencies> <dependency> <groupId>org.json</groupId> <artifactId>json</artifactId> <version>20160810</version> </dependency>查看全部
-
使用Map實(shí)現(xiàn)Json private static void createJsonByMap(){ Map<String, Object> wangxiaoer=new HashMap<String,Object>(); Object nullObj = null; wangxiaoer.put("name", "王小二"); wangxiaoer.put("age", 25.2); wangxiaoer.put("birthday", "1990-01-01"); wangxiaoer.put("school", "藍(lán)翔"); wangxiaoer.put("major", new String[] { "理發(fā)", "挖掘機(jī)" }); wangxiaoer.put("has_girlfriend", false); wangxiaoer.put("car", nullObj); wangxiaoer.put("house", nullObj); wangxiaoer.put("comment", "這是一個(gè)注釋"); System.out.println(new JSONObject(wangxiaoer).toString()); }查看全部
-
引入依賴 <dependency> <groupId>org.json</groupId> <artifactId>json</artifactId> <version>20090211</version> </dependency> 使用JsonObject實(shí)現(xiàn)Json package json; import org.json.JSONException; import org.json.JSONObject; public class JSONObjectSample { public static void main(String[] args) { // TODO 自動(dòng)生成的方法存根 JSONObject(); } private static void JSONObject() { // TODO 自動(dòng)生成的方法存根 JSONObject wangxiaoer = new JSONObject(); Object nullObj = null; try { wangxiaoer.put("name", "王小二"); wangxiaoer.put("age", 25.2); wangxiaoer.put("birthday", "1990-01-01"); wangxiaoer.put("school", "藍(lán)翔"); wangxiaoer.put("major", new String[] { "理發(fā)", "挖掘機(jī)" }); wangxiaoer.put("has_girlfriend", false); wangxiaoer.put("car", nullObj); wangxiaoer.put("house", nullObj); wangxiaoer.put("comment", "這是一個(gè)注釋"); System.out.println(wangxiaoer.toString()); } catch (JSONException e) { // TODO: handle exception e.printStackTrace(); } } }查看全部
-
{ "name": "王小二", "age": 25.2, "birthday": "1990-01-01", "school": "藍(lán)翔", "major": [ "理發(fā)", "挖掘機(jī)" ], "has_girlfriend": false, "car": null, "house": null, "comment": "這是一個(gè)注釋" }查看全部
-
數(shù)據(jù)結(jié)構(gòu):Object、Array 基本類型:string,number,true,false,null (1)Object {key:value,key:value...} key:string類型。 value:任何基本類型或數(shù)據(jù)結(jié)構(gòu)。 (2)Array [value,value...] value:任何基本類型或數(shù)據(jù)結(jié)構(gòu)。查看全部
-
json是一種與開發(fā)語言無關(guān)的,輕量級(jí)的數(shù)據(jù)格式,全稱:javascript object notation JSON的樣例: { "name":"Json快速入門(Java版)", "author":"李廣", "content":["JSON基礎(chǔ)入門","常用JSON處理"], "time":{"value":30, "unit":"分鐘" } }查看全部
-
JSON是當(dāng)前行業(yè)內(nèi)使用最為廣泛的一種數(shù)據(jù)傳輸格式,是開發(fā)人員必備技能之一 選擇JSON 可以做為一種數(shù)據(jù)返回格式,也可以做為一種數(shù)據(jù)存數(shù)格式 大多數(shù)API 用json作為返回格式 數(shù)據(jù)庫 也會(huì)用json格式進(jìn)行數(shù)據(jù)存儲(chǔ) json提供了一種對(duì)象序列化的方式查看全部
-
直接new一個(gè)jsond對(duì)象,它的構(gòu)造函數(shù)中有直接傳遞map集合的參數(shù)查看全部
-
json的數(shù)據(jù)表示查看全部
-
這是什么查看全部
-
Json-java 庫 是使用反射類的Method獲取信息來構(gòu)建json的;而Gson是使用反射類的Field信息來構(gòu)建json的。查看全部
-
Json解析 public class ReadJsonSample { //反向解析為一個(gè)json public static void main(String[] args)throws Exception{ File file=new File(ReadJsonSample.class.getResource("/data/terence.json").getFile()); String content=FileUtils.readFileToString(file); JSONObject jsonObject=new JSONObject(content); if(!jsonObject.isNull("name")) { System.out.println("姓名:"+jsonObject.getString("name")); } System.out.println("年齡:"+jsonObject.getDouble("age")); } }查看全部
-
使用Bean實(shí)現(xiàn)Json Bean Class: public class DaShen { private Stringname; private Stringschool; private boolean has_girlfriend; private double age; private Objectcar; private Objecthouse; private String[]major; private Stringcomment; private String birthday; } 實(shí)現(xiàn): private static void createJsonByBean() { DaShen terence=newDaShen(); terence.setAge(25.9); terence.setBirthday("1990-5-9"); terence.setSchool("HDU"); terence.setMajor(new String[]{"Computer","qiqiqiqi"}); terence.setHas_girlfriend(false); terence.setComment("sha,sha,sha,sha……"); terence.setCar(null); terence.setHouse(null); System.out.println(new JSONObject(terence)); }查看全部
舉報(bào)
0/150
提交
取消