javascript
JSON总结(java篇)
JSON總結(jié)(java篇一)
JSON簡(jiǎn)介
JSON(JavaScript Object Notation)
是一種輕量級(jí)的數(shù)據(jù)交換格式。它基于ECMAScript的一個(gè)子集。 JSON采用完全獨(dú)立于語言的文本格式,但是也使用了類似于C語言家族的習(xí)慣(包括C、C++、C#、Java、JavaScript、Perl、Python等)。這些特性使JSON成為理想的數(shù)據(jù)交換語言。 易于人閱讀和編寫,同時(shí)也易于機(jī)器解析和生成(一般用于提升網(wǎng)絡(luò)傳輸速率)。
JSON語法
- 數(shù)據(jù)在鍵值對(duì)中
- 數(shù)據(jù)由逗號(hào)分隔
- 花括號(hào)保存對(duì)象
方括號(hào)保存數(shù)組
JSON值類型
- number(整數(shù)或浮點(diǎn)數(shù))
- String(字符串,在雙引號(hào)中)
- boolean(布爾值,true或false)
- array(數(shù)組,用方括號(hào)表示)
- Object(對(duì)象,在花括號(hào)中,例如javaScript對(duì)象)
null
java中的用法
準(zhǔn)備工作
點(diǎn)擊后進(jìn)入github下載頁(yè)面下載jar包。
JSON的使用
1. 使用Map集合創(chuàng)建JSON
/*** 1 、使用Map創(chuàng)建json*/public static void createJSONByMap() {Map<String, Object> map = new LinkedHashMap<String, Object>();map.put("name", "老王");map.put("age", 35);map.put("height", 1.73);map.put("major", new String[] { "理發(fā)", "挖掘機(jī)" });map.put("hasGirlFriend", false);map.put("car", null);JSONObject json = new JSONObject(map);System.out.println("方法名:createJSONByMap()---" + json);}運(yùn)行結(jié)果:
方法名:createJSONByMap()---{"major":["理發(fā)","挖掘機(jī)"],"name":"老王","hasGirlFriend":false,"age":35,"height":1.73}從結(jié)果可以看出,car的字段沒有打印出來,說明當(dāng)value為null時(shí)轉(zhuǎn)換JSON后不會(huì)顯示出來
2. 使用javaBean創(chuàng)建JSON
javaBean
package com.hjw.maven.jsonTest;import java.util.Arrays;/***@author hjw*@version 創(chuàng)建時(shí)間:2016年9月21日 上午11:23:51* TODO*/ public class Person {private String name;private int age;private double height;private String[] major;private boolean hasGirlFriend;public String getName() {return name;}public void setName(String name) {this.name = name;}public int getAge() {return age;}public void setAge(int age) {this.age = age;}public double getHeight() {return height;}public void setHeight(double height) {this.height = height;}public String[] getMajor() {return major;}public void setMajor(String[] major) {this.major = major;}public boolean isHasGirlFriend() {return hasGirlFriend;}public void setHasGirlFriend(boolean hasGirlFriend) {this.hasGirlFriend = hasGirlFriend;}@Overridepublic String toString() {return "Person [name=" + name + ", age=" + age + ", height=" + height+ ", major=" + Arrays.toString(major) + ", hasGirlFriend="+ hasGirlFriend + "]";}} /*** 2、通過javaBean創(chuàng)建json*/public static void createJSONByBean() {Person person = new Person();person.setName("老王");person.setAge(35);person.setHasGirlFriend(false);person.setHeight(17.2);person.setMajor(new String[] { "廚師", "編程" });System.out.println("方法名:createJSONByBean()---" + new JSONObject(person));}方法名:createJSONByBean()---{"major":["廚師","編程"],"name":"老王","hasGirlFriend":false,"age":35,"height":17.2}
3. 通過JSONObject創(chuàng)建JSON
/*** 1、通過JSONObject來專家json*/public static void createJSON() {JSONObject json = new JSONObject();Object objNull = null;json.put("name", "老王");json.put("age", 35);json.put("height", 1.73);json.put("major", new String[] { "理發(fā)", "挖掘機(jī)" });json.put("hasGrilFriend", false);System.out.println("方法名:createJSON1()---" + json);}方法名:createJSON()---{"hasGrilFriend":false,"major":["理發(fā)","挖掘機(jī)"],"name":"老王","age":35,"height":1.73}
4. 讀取文件創(chuàng)建JSONObject
在maven項(xiàng)目src/main/resource中創(chuàng)建laowang.json文件,然后引入commons-io的maven坐標(biāo)
laowang.json {"hasGrilFriend": false,"major": ["理發(fā)","挖掘機(jī)"],"name": "老王","age": 35,"height": 1.73 }代碼:
/*** 4、讀取文件獲取json* * @throws IOException*/public static void createJsonByFile() throws IOException {File file = new File(JsonDemo.class.getResource("/laowang.json").getFile());String content = FileUtils.readFileToString(file);JSONObject json = new JSONObject(content);System.out.println("name=" + json.getString("name"));System.out.println("age=" + json.getInt("age"));System.out.println("height=" + json.getDouble("height"));System.out.println("hasGirlFriend=" + json.getBoolean("hasGirlFriend"));System.out.print("major=[");for (Object str : json.getJSONArray("major")) {System.out.print(str + ",");}System.out.println("]");}運(yùn)行結(jié)果:
name=老王
age=35
height=1.73
hasGirlFriend=false
major=[理發(fā),挖掘機(jī),]
5. 通過JSONObject創(chuàng)建json文件
/*** 創(chuàng)建josn文件* * @throws IOException*/public static void createJsonFileByWriter() throws IOException {Map<String, Object> map = new LinkedHashMap<String, Object>();map.put("name", "老王");map.put("age", 35);map.put("height", 1.73);map.put("major", new String[] { "理發(fā)", "挖掘機(jī)" });map.put("hasGrilFriend", false);JSONObject json = new JSONObject(map);URL url=JsonDemo.class.getResource("/");String path=url.getPath();path=path.substring(0, path.indexOf("jsonTest"));File file = new File(path+"/jsonTest/src/main/resource/laowang1.json");if (!file.exists()) {file.createNewFile();}FileWriter fw = new FileWriter(file.getAbsoluteFile());BufferedWriter bw = new BufferedWriter(fw);json.write(bw);bw.close();System.out.println("end");}代碼運(yùn)行后會(huì)自動(dòng)在maven項(xiàng)目中的resource路徑下生產(chǎn)一個(gè)名為laowang1.json的文件,其中jsonTest為項(xiàng)目名
轉(zhuǎn)載于:https://www.cnblogs.com/gongchenglion/p/5892967.html
總結(jié)
以上是生活随笔為你收集整理的JSON总结(java篇)的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 窗口迅速关闭的解决办法/scanf/if
- 下一篇: 分析Crash文件