读取json

来源:互联网 发布:国金证券知乎 编辑:程序博客网 时间:2024/05/17 22:18

1.创建maven

2.添加依赖


3.代码

package Json;


import java.io.File;
import java.io.IOException;


import org.apache.commons.io.FileUtils;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;


public class ReadJSONSample {
public static void main(String[] args) throws IOException, JSONException {


File file = new File(ReadJSONSample.class.getResource(
"/wangxiaoer.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"));
JSONArray jsonArray = jsonObject.getJSONArray("major");


}


}

原创粉丝点击