1.使用Map集合建立JSONjava
/** * 1 、使用Map建立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[] { "理髮", "挖掘機" }); map.put("hasGirlFriend", false); map.put("car", null); JSONObject json = new JSONObject(map); System.out.println("方法名:createJSONByMap()---" + json); }
運行結果:
方法名:createJSONByMap()---{"major":["理髮","挖掘機"],"name":"老王","hasGirlFriend":false,"age":35,"height":1.73}
2.使用javaBean建立JSON編程
package com.hjw.maven.jsonTest; import java.util.Arrays; 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; } @Override public String toString() { return "Person [name=" + name + ", age=" + age + ", height=" + height + ", major=" + Arrays.toString(major) + ", hasGirlFriend=" + hasGirlFriend + "]"; } }
/** * 二、經過javaBean建立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)); }
3.經過JSONObject建立JSONjson
/** * 一、經過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[] { "理髮", "挖掘機" }); json.put("hasGrilFriend", false); System.out.println("方法名:createJSON1()---" + json); }
4.讀取文件建立JSONObjectruby
在maven項目src/main/resource中建立laowang.json文件,而後引入commons-io的maven座標maven
laowang.json { "hasGrilFriend": false, "major": [ "理髮", "挖掘機" ], "name": "老王", "age": 35, "height": 1.73 }
/** * 四、讀取文件獲取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("]"); } 運行結果: name=老王 age=35 height=1.73 hasGirlFriend=false major=[理髮,挖掘機,]
5.經過JSONObject建立json文件ide
/** * 建立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[] { "理髮", "挖掘機" }); 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"); }
代碼運行後會自動在maven項目中的resource路徑下生產一個名爲laowang1.json的文件,其中jsonTest爲項目名this