JSON: Tạo JSON File & đọc Nội Dung JSON File Thông Tin Cá Nhân ...

NguyenHuuThanh

2020-04-22 05:47:00

/* * To change this license header, choose License Headers in Project Properties. * To change this template file, choose Tools | Templates * and open the template in the editor. */ package javajsono; import java.util.ArrayList; import java.util.List; import org.json.JSONArray; import org.json.JSONObject; /** * * @author Minh */ public class Student { String name , address ; int age ; List<Hobby> hobby_list ; List<Language> language_list ; public Student() { hobby_list = new ArrayList(); language_list = new ArrayList(); } public String getName() { return name; } public void setName(String name) { this.name = name; } public String getAddress() { return address; } public void setAddress(String address) { this.address = address; } public int getAge() { return age; } public void setAge(int age) { this.age = age; } public List<Hobby> getHobby_list() { return hobby_list; } public void setHobby_list(List<Hobby> hobby_list) { this.hobby_list = hobby_list; } public List<Language> getLanguage_list() { return language_list; } public void setLanguage_list(List<Language> language_list) { this.language_list = language_list; } public void display() { System.out.println("Student :" + name + " ,address" + address + ", age" + age); for (Hobby hobby : hobby_list) { System.out.println(hobby); } for (Language language : language_list) { System.out.println(language); } } public void convertJSON(JSONObject obj) { name = obj.getString("name"); address = obj.getString("address"); age = obj.getInt("age"); JSONArray arr1 = obj.getJSONArray("hobby_list"); JSONArray arr2 = obj.getJSONArray("language_list"); for ( int i = 0 ; i < arr1.length() ; i++) { JSONObject hobObj = arr1.getJSONObject(i); Hobby hobby = new Hobby(); hobby.convertJSON(hobObj); hobby_list.add(hobby); } for ( int j = 0 ; j < arr2.length() ; j++) { JSONObject langObj = arr2.getJSONObject(j); Language language = new Language(); language.convertJSON(langObj); language_list.add(language); } } } /* * To change this license header, choose License Headers in Project Properties. * To change this template file, choose Tools | Templates * and open the template in the editor. */ package javajsono; import java.io.FileInputStream; import java.io.FileNotFoundException; import java.io.IOException; import java.util.logging.Level; import java.util.logging.Logger; import org.json.JSONArray; import org.json.JSONObject; /** * * @author Minh */ public class JavaJSONo { /** * @param args the command line arguments */ public static void main(String[] args) { // TODO code application logic here FileInputStream fis = null; Student student = new Student(); try { fis = new FileInputStream("student.json"); StringBuilder builder = new StringBuilder(); int code ; while (( code = fis.read()) != -1 ) { builder.append((char) code ); } String content = builder.toString(); JSONArray array = new JSONArray(content); for ( int i = 0 ; i < array.length() ; i++) { JSONObject obj = array.getJSONObject(i); student.convertJSON(obj); } student.display(); } catch (FileNotFoundException ex) { Logger.getLogger(JavaJSONo.class.getName()).log(Level.SEVERE, null, ex); } catch (IOException ex) { Logger.getLogger(JavaJSONo.class.getName()).log(Level.SEVERE, null, ex); } finally { if (fis != null) { try { fis.close(); } catch (IOException ex) { Logger.getLogger(JavaJSONo.class.getName()).log(Level.SEVERE, null, ex); } } } } } /* * To change this license header, choose License Headers in Project Properties. * To change this template file, choose Tools | Templates * and open the template in the editor. */ package javajsono; import org.json.JSONObject; /** * * @author Minh */ public class Hobby { String name , desc ; public Hobby() { } public String getName() { return name; } public void setName(String name) { this.name = name; } public String getDesc() { return desc; } public void setDesc(String desc) { this.desc = desc; } @Override public String toString() { return "Language{" + "name=" + name + ", desc=" + desc + '}'; } public void convertJSON(JSONObject obj) { name = obj.getString("name"); desc = obj.getString("desc"); } } /* * To change this license header, choose License Headers in Project Properties. * To change this template file, choose Tools | Templates * and open the template in the editor. */ package javajsono; import org.json.JSONObject; /** * * @author Minh */ public class Language { String name , desc ; public Language() { } public String getName() { return name; } public void setName(String name) { this.name = name; } public String getDesc() { return desc; } public void setDesc(String desc) { this.desc = desc; } @Override public String toString() { return "Language{" + "name=" + name + ", desc=" + desc + '}'; } public void convertJSON(JSONObject obj) { name = obj.getString("name"); desc = obj.getString("desc"); } } [ { "name" : "Nguyen Van A" , "age" : 19 , "address" : "Ha Noi" , "hobby_list" : [ { "name" : "football" , "desc" : "football" }, { "name" : "basketball" , "desc" : "basketball" } ] , "language_list" : [ { "name" : "C#" , "desc" : "C#" }, { "name" : "Java" , "desc" : "Java" } ] } ]

Từ khóa » đọc File Json Trong Java