1.使用Map集合创建JSON
/**
* 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 + "]";
}
}
/**
* 2、通过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创建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[] { "理发", "挖掘机" });
json.put("hasGrilFriend", false);
System.out.println("方法名:createJSON1()---" + json);
}
4.读取文件创建JSONObject
在maven项目src/main/resource中创建laowang.json文件,然后引入commons-io的maven坐标
laowang.json
{
"hasGrilFriend": false,
"major": [
"理发",
"挖掘机"
],
"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("]");
}
运行结果:
name=老王
age=35
height=1.73
hasGirlFriend=false
major=[理发,挖掘机,]
5.通过JSONObject创建json文件
/**
* 创建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为项目名