雷区:1、json的要求字符集必须是Unicode,如果为其他编码格式会产生空、乱码等问题。
josn.org工程包:创建json对象三种方式
//三种创建json对象的方式
public static void main(String[] args) {
jSONObjectSample();
createJSONObjectByMap();
createJSONObjectByBean();
}
//直接new
public static void jSONObjectSample() {
JSONObject fangao = new JSONObject();
Object nullObj = null;
fangao.put("name", "梵高");
fangao.put("age", 26);
fangao.put("birthday", "1853-3-30");
fangao.put("major",new String[] {"牧师","画家"});
fangao.put("has_girlfriend", false);
fangao.put("car", nullObj);
fangao.put("house", nullObj);
fangao.put("comment", "伟大的画家,悲催的人生");
System.out.println(fangao.toString());
}
//map集合
public static void createJSONObjectByMap() {
Map<String, Object> fangao = new HashMap<String, Object>();
Object nullObj = null;
fangao.put("name", "梵高");
fangao.put("age", 26);
fangao.put("birthday", "1853-3-30");
fangao.put("major",new String[] {"牧师","画家"});
fangao.put("has_girlfriend", false);
fangao.put("car", nullObj);
fangao.put("house", nullObj);
fangao.put("comment", "伟大的画家,悲催的人生");
System.out.println(new JSONObject(fangao).toString());
}
//javabean
public static void createJSONObjectByBean() {
Person fangao = new Person();
fangao.setName("梵高");
fangao.setAge(26);
fangao.setBirthday("1853-3-30");
fangao.setMajor(new String[] {"牧师","画家"});
fangao.setHas_girlfriend(false);
fangao.setCar(null);
fangao.setHouse(null);
fangao.setComment("伟大的画家,悲催的人生");
System.out.println(new JSONObject(fangao).toString());
}
读取文件中的json
import java.io.File;
import java.io.IOException;
import org.apache.commons.io.FileUtils;
import org.json.JSONObject;
public class ReadJSONSample {
public static void main(String[] args) throws IOException {
File file = new File(ReadJSONSample.class.getResource("/fangao.json").getFile());
String content = FileUtils.readFileToString(file);
JSONObject jsonObject = new JSONObject(content);
System.out.println(jsonObject.getString("name"));
}
}
{
"birthday": "1853-3-30",
"major": [
"牧师",
"画家"
],
"name": "梵高",
"has_girlfriend": false,
"comment": "伟大的画家,悲催的人生",
"age": 26
}
gson工程包
public class Person {
private String name;
private double age;
private String birthday;
private String[] major;
private boolean has_girlfriend;
private Object car;
private Object house;
private String comment;
private transient String ignore; //gson生成json对象时忽略这个字段
}
public static void createJSONObjectByGson() {
Person fangao = new Person();
fangao.setName("梵高");
fangao.setAge(26);
fangao.setBirthday("1853-3-30");
fangao.setMajor(new String[] { "牧师", "画家" });
fangao.setHas_girlfriend(false);
fangao.setCar(null);
fangao.setHouse(null);
fangao.setComment("伟大的画家,悲催的人生");
fangao.setIgnore("看不到我");
//使用GsonBuilder可以在生成json对象之前做一些定制设置
GsonBuilder gsonBuilder = new GsonBuilder();
gsonBuilder.setPrettyPrinting();
gsonBuilder.setFieldNamingStrategy(new FieldNamingStrategy() {
public String translateName(Field f) {
if (f.getName().equals("name")) {
return "NAME";
}
return f.getName();
}
});
Gson gson = gsonBuilder.create();
System.out.println(gson.toJson(fangao));
}
依赖的jar包
<!-- https://mvnrepository.com/artifact/org.json/json -->
<dependency>
<groupId>org.json</groupId>
<artifactId>json</artifactId>
<version>20160810</version>
</dependency>
<!-- https://mvnrepository.com/artifact/commons-io/commons-io -->
<dependency>
<groupId>commons-io</groupId>
<artifactId>commons-io</artifactId>
<version>2.4</version>
</dependency>
<!-- https://mvnrepository.com/artifact/com.google.code.gson/gson -->
<dependency>
<groupId>com.google.code.gson</groupId>
<artifactId>gson</artifactId>
<version>2.8.0</version>
</dependency>
附件资料: