javascript
JSON——Java中的使用
1. 構建JSON方法(數據——>JSON)
這里使用Maven構建項目
在pom.xml中添加如下依賴
<dependency><groupId>org.json</groupId><artifactId>json</artifactId><version>20090211</version></dependency>1.1 創建JSONObject對象,利用put(key,value)賦值,toString() 打印出JSON格式
關鍵詞:JSONObject對象,put(), toString()
public class JsonObjectSimple {public static void main(String[] args) {jSONObjectSimple();}private static void jSONObjectSimple() {JSONObject xiaofeng=new JSONObject();Object nullObj=null;//因為put()方法的原因,這里不能直接使用null,所以創建null的對象來跳過編譯器的檢查try {xiaofeng.put("name", "小峰");xiaofeng.put("age", 22);xiaofeng.put("birthday", "1999-11-22");xiaofeng.put("school", "Qinghua University");xiaofeng.put("major", new String[] {"sing","coding"});xiaofeng.put("girlfriend", "true");xiaofeng.put("car",nullObj); //不能直接使用null,需要創建null的對象來跳過編譯器的檢查xiaofeng.put("comment","JSON里不能直接使用注釋,需要添加時可通過此方式。。");} catch (JSONException e) {// TODO Auto-generated catch block e.printStackTrace();} System.out.println(xiaofeng.toString()); } }控制臺輸出后復制其到?http://www.jsoneditoronline.org/?可查看 JSON 數據結構
1.2 通過 HashMap 構建
?關鍵詞:HashMap() , put() , toString() ,?JSONObject(xiaofeng)
private static void createJSONByMap() {Map<String,Object> xiaofeng=new HashMap<String,Object>();Object nullObj=null;xiaofeng.put("name", "小峰");xiaofeng.put("age", 22);xiaofeng.put("birthday", "1999-11-22");xiaofeng.put("school", "Qinghua University");xiaofeng.put("major", new String[] {"sing","coding"});xiaofeng.put("girlfriend", "true");xiaofeng.put("car",nullObj); //不能直接使用null,需要創建null的對象來跳過編譯器的檢查xiaofeng.put("comment","JSON里不能直接使用注釋,需要添加時可通過此方式。。");System.out.println(new JSONObject(xiaofeng).toString());}3. 使用 JavaBean 創建 JSON
關鍵詞:JavaBean, ?setXxx(), ?JSONObject(xiaofeng)
?首先創建 JavaBean 類Person(略), ?之后創建。。。
private static void createJSONByBean() {//創建Person對象,利用set()方法賦值,最后轉為JSONObject對象輸出Person xiaofeng=new Person();xiaofeng.setName("小峰");xiaofeng.setAge(22.5);xiaofeng.setGirlfriend(true);xiaofeng.setMajor(new String[]{"唱歌","coding"});System.out.println(new JSONObject(xiaofeng)); }注意,在創建JavaBean時,由于JSON不支持date格式,所以日期格式需要設置為String類型,這也是JSON的缺陷。
?
2. 解析讀取JSON數據(JSON——>數據)
xiaofeng.json {"birthday": "1999-11-22","girlfriend": "true","major": ["sing","coding"],"school": "Qinghua University","car": null,"name": "小峰","comment": "JSON里不能直接使用注釋,需要添加時可通過此方式。。","age": 22 }從文件中讀取JSON
關鍵詞:
ReadJSON.class.getResource("/xiaofeng.json").getFile() ,JSONArray,readFileToString(file)
?
public class ReadJSON {public static void main(String[] args) throws IOException, JSONException {//獲取本文件路徑下的json文件File file=new File(ReadJSON.class.getResource("/xiaofeng.json").getFile());//讀取json文件內容String content=FileUtils.readFileToString(file);JSONObject jsonObject =new JSONObject(content);System.out.println("姓名是 :"+jsonObject.getString("name"));System.out.println("年齡是 :"+jsonObject.getDouble("age"));System.out.println("有女朋友嗎 ?"+jsonObject.getBoolean("girlfriend"));
//數組類型轉換成JSONArray類型來解析,不能直接讀取JSONArray majorArray=jsonObject.getJSONArray("major");for(int i=0;i<majorArray.length();i++){String m=(String) majorArray.get(i);System.out.println("專業——"+(i+1)+m);}}}
控制臺輸出
為增加程序健壯性,可在JSON數據解析時加入 非空【isNull()】 判斷
//判斷 name 是否為空if (!jsonObject.isNull("name")) {System.out.println("姓名是 :" + jsonObject.getString("name"));}//反例,無輸出if (!jsonObject.isNull("nme")) {System.out.println("姓名是 :" + jsonObject.getString("name"));}System.out.println("年齡是 :" + jsonObject.getDouble("age"));?
總結
以上是生活随笔為你收集整理的JSON——Java中的使用的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: php——数据库操作之规范性
- 下一篇: Citrix Provisioning