27/09/2018, 14:07
JSON.simple – phân tích cú pháp JSON trong Java
Collection trong java Một ví dụ tiếp theo của bài Ví dụ JSON với Java – json.simple. Ví dụ: phân tích cú pháp JSON trong Java, với chuỗi JSON được đọc từ một file: File employee.json: { "firstName":"Vinh", "lastName":"Phan&qu ...
Collection trong java
Một ví dụ tiếp theo của bài Ví dụ JSON với Java – json.simple.
Ví dụ: phân tích cú pháp JSON trong Java, với chuỗi JSON được đọc từ một file:
File employee.json:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 | { "firstName" : "Vinh" , "lastName" : "Phan" , "address" :{ "streetAddress" : "11 Tu Lap" , "district" : "Me Linh" , "city" : "Ha Noi" , "state" : "" , "postalCode" : "100000" }, "age" : 25 , "phoneNumbers" :[ { "type" : "home" , "number" : "096677028" }, { "type" : "fax" , "number" : "0435508028" } ], "sex" : "Male" , "salary" : 1200.0 } |
File JSONSimpleExample.java
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 | package vn.viettuts.json; import java.io.FileNotFoundException; import java.io.FileReader; import java.io.IOException; import java.util.Iterator; import java.util.Map; import org.json.simple.JSONArray; import org.json.simple.JSONObject; import org.json.simple.parser.JSONParser; import org.json.simple.parser.ParseException; public class JSONSimpleExample { public static void main(String[] args) { Object obj; try { obj = new JSONParser().parse( new FileReader( "D:\json\employee.json" )); JSONObject jsonObject = (JSONObject) obj; // đọc firstName và lastName String firstName = (String) jsonObject.get( "firstName" ); String lastName = (String) jsonObject.get( "lastName" ); System.out.println( "firstName: " + firstName); System.out.println( "lastName: " + lastName); // đọc age long age = (Long) jsonObject.get( "age" ); System.out.println( "age:" + age); // đọc address Map address = ((Map) jsonObject.get( "address" )); // đọc address Map Iterator<Map.Entry> itr1 = address.entrySet().iterator(); while (itr1.hasNext()) { Map.Entry pair = itr1.next(); System.out.println(pair.getKey() + " : " + pair.getValue()); } // đọc phoneNumbers JSONArray jaPhoneNumbers = (JSONArray) jsonObject.get( "phoneNumbers" ); // đọc mảng phoneNumbers Iterator itr2 = jaPhoneNumbers.iterator(); while (itr2.hasNext()) { itr1 = ((Map) itr2.next()).entrySet().iterator(); while (itr1.hasNext()) { Map.Entry pair = itr1.next(); System.out.println(pair.getKey() + " : " + pair.getValue()); } } String sex = (String) jsonObject.get( "sex" ); System.out.println( "sex: " + sex); Double salary = (Double) jsonObject.get( "salary" ); System.out.println( "salary: " + salary); } catch (FileNotFoundException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } catch (ParseException e) { e.printStackTrace(); } } } |
Kết quả:
firstName: Vinh lastName: Phan age:25 streetAddress : 11 Tu Lap city : Ha Noi district : Me Linh postalCode : 100000 state : number : 096677028 type : home number : 0435508028 type : fax sex:Male salary1200.0
Collection trong java