By GokiSoft.com| 20:10 05/08/2022|
Java Advanced

JSON: Tạo JSON file & đọc nội dung JSON file thông tin cá nhân bằng Java BT1209

Viết tài liệu JSON mô tả thông tin cá nhân, sở thích, dụng cụ học tập, ngôn ngữ lập trình bạn thích

Yêu cầu: Khi đọc tài liệu có thể biết được profile của 1 cá nhân duy nhất, xem được thông tin cá nhân, sở thích, ngôn ngữ lập trình của cá nhân đó và các trang thiết bị học tập. Các thẻ phải mô tả chính xác nội dung đang cần diễn đạt (cách đặt tên)


Viết chương trình phân tích tài liệu trên bằng Java

Liên kết rút gọn:

https://gokisoft.com/1209

Bình luận

avatar
Đào Mạnh Dũng [C2010L]
2021-09-21 16:11:15


#File.java


/*
 * 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 Utility;

import java.io.BufferedReader;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.FileReader;
import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.util.logging.Level;
import java.util.logging.Logger;

/**
 *
 * @author inter
 */
public class File {
    public static String getContentFile(String filename) {
        StringBuilder builder = new StringBuilder();
        
        FileReader reader = null;
        BufferedReader bufferedReader = null;
        
        try {
            reader = new FileReader(filename);
            bufferedReader = new BufferedReader(reader);
            
            String line = "";
            while((line = bufferedReader.readLine()) != null) {
                builder.append(line);
            }
        } catch (FileNotFoundException ex) {
            Logger.getLogger(File.class.getName()).log(Level.SEVERE, null, ex);
        } catch (IOException ex) {
            Logger.getLogger(File.class.getName()).log(Level.SEVERE, null, ex);
        } finally {
            if(reader != null) {
                try {
                    reader.close();
                } catch (IOException ex) {
                    Logger.getLogger(File.class.getName()).log(Level.SEVERE, null, ex);
                }
            }
            if(bufferedReader != null) {
                try {
                    bufferedReader.close();
                } catch (IOException ex) {
                    Logger.getLogger(File.class.getName()).log(Level.SEVERE, null, ex);
                }
            }
        }
        
        return builder.toString();
    }
    
    public static void saveFile(String content, String filename) {
        FileOutputStream fos = null;
        
        try {
            fos = new FileOutputStream(filename);
            
            byte[] data = content.getBytes("utf8");
            
            fos.write(data);
        } catch (FileNotFoundException ex) {
            Logger.getLogger(File.class.getName()).log(Level.SEVERE, null, ex);
        } catch (UnsupportedEncodingException ex) {
            Logger.getLogger(File.class.getName()).log(Level.SEVERE, null, ex);
        } catch (IOException ex) {
            Logger.getLogger(File.class.getName()).log(Level.SEVERE, null, ex);
        } finally {
            if(fos != null) {
                try {
                    fos.close();
                } catch (IOException ex) {
                    Logger.getLogger(File.class.getName()).log(Level.SEVERE, null, ex);
                }
            }
        }
    }
}


#Main.java


/*
 * 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 pkg1209;

import org.json.JSONObject;

/**
 *
 * @author inter
 */
public class Main {

    /**
     * @param args the command line arguments
     */
    public static void main(String[] args) {
        String content = Utility.File.getContentFile("profile.json");
        
        JSONObject obj = new JSONObject(content);
        profile Profile = new profile(obj.getJSONObject("profile"));
        
        System.out.println(Profile);
    }
    
}


#profile.java


/*
 * 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 pkg1209;

import org.json.JSONObject;

/**
 *
 * @author inter
 */
public class profile {

    String fullname, gender, email, phone_number, learning_tools, hobby, programming_language;

    public profile() {
    }

    public profile(String fullname, String gender, String email, String phone_number, String learning_tools, String hobby, String programming_language) {
        this.fullname = fullname;
        this.gender = gender;
        this.email = email;
        this.phone_number = phone_number;
        this.learning_tools = learning_tools;
        this.hobby = hobby;
        this.programming_language = programming_language;
    }

    public profile(JSONObject jsonObject) {
        this.fullname = jsonObject.getString("full-name");
        this.gender = jsonObject.getString("gender");
        this.email = jsonObject.getString("email");
        this.phone_number = jsonObject.getString("phone-number");
        this.learning_tools = jsonObject.getString("learning-tools");
        this.hobby = jsonObject.getString("hobby");
        this.programming_language = jsonObject.getString("programming-language");
    }

    public String getFullname() {
        return fullname;
    }

    public void setFullname(String fullname) {
        this.fullname = fullname;
    }

    public String getGender() {
        return gender;
    }

    public void setGender(String gender) {
        this.gender = gender;
    }

    public String getEmail() {
        return email;
    }

    public void setEmail(String email) {
        this.email = email;
    }

    public String getPhone_number() {
        return phone_number;
    }

    public void setPhone_number(String phone_number) {
        this.phone_number = phone_number;
    }

    public String getLearning_tools() {
        return learning_tools;
    }

    public void setLearning_tools(String learning_tools) {
        this.learning_tools = learning_tools;
    }

    public String getHobby() {
        return hobby;
    }

    public void setHobby(String hobby) {
        this.hobby = hobby;
    }

    public String getProgramming_language() {
        return programming_language;
    }

    public void setProgramming_language(String programming_language) {
        this.programming_language = programming_language;
    }

    @Override
    public String toString() {
        return "profile{" + "fullname=" + fullname + ", gender=" + gender + ", email=" + email + ", phone_number=" + phone_number + ", learning_tools=" + learning_tools + ", hobby=" + hobby + ", programming_language=" + programming_language + '}';
    }

}


avatar
Thành Lâm [T1907A]
2020-04-23 20:30:44



[
  {
    "fullname": "Nguyen Thanh Lam",
    "age": "19",
    "gender": "Male",
    "email": "lamnguyen14777@gmail.com",
    "address": "Hai Phong",
    "hobbly_list": [
      "hobbly 1: football",
      "hobbly 2: GYM"
    ],
    "language?_list": [
      "HTML",
      "CSS",
      "JS",
      "SQL"
    ],

  }
]



/*
 * 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 lesson3;

import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.util.ArrayList;
import java.util.logging.Level;
import java.util.logging.Logger;
import java.lang.reflect.Type;
package lesson3;

import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.util.ArrayList;
import java.util.logging.Level;
import java.util.logging.Logger;
import java.lang.reflect.Type;

/**
 *
 * @author
 */
public class Main {
    public static void main(String[] args) {
        FileReader reader = null;
        try {
            reader = new FileReader("YourSelf.json");
            
            Type classOfT = new TypeToken<ArrayList<YourSelf>>(){}.getType();
            
            Gson gson = new Gson();
            ArrayList<YourSelf> list =  gson.fromJson(reader, classOfT);
            
            for (YourSelf yourSelf : list) {
                yourSelf.display();
            }
        } catch (FileNotFoundException ex) {
            Logger.getLogger(Main.class.getName()).log(Level.SEVERE, null, ex);
        }finally{
         if(reader != null){
             try {
                 reader.close();
             } catch (IOException ex) {
                 Logger.getLogger(Main.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 lesson3;

import java.util.ArrayList;
import org.json.JSONArray;
import org.json.JSONObject;

/**
 *
 * @author 
 */
public class YourSelf {
    String fullname,address,gender;
    int age;
    ArrayList<String> hobbielist = new ArrayList<>();
    ArrayList<String> learningtoollist = new ArrayList<>();
    ArrayList<String> programlanguagelist = new ArrayList<>();

    public YourSelf() {
    }

    public YourSelf(String fullname, String address, String gender, int age) {
        this.fullname = fullname;
        this.address = address;
        this.gender = gender;
        this.age = age;
    }

    public String getFullname() {
        return fullname;
    }

    public void setFullname(String fullname) {
        this.fullname = fullname;
    }

    public String getAddress() {
        return address;
    }

    public void setAddress(String address) {
        this.address = address;
    }

    public String getGender() {
        return gender;
    }

    public void setGender(String gender) {
        this.gender = gender;
    }

    public int getAge() {
        return age;
    }

    public void setAge(int age) {
        this.age = age;
    }

    public ArrayList<String> getHobbielist() {
        return hobbielist;
    }

    public void setHobbielist(ArrayList<String> hobbielist) {
        this.hobbielist = hobbielist;
    }

    public ArrayList<String> getLearningtoollist() {
        return learningtoollist;
    }

    public void setLearningtoollist(ArrayList<String> learningtoollist) {
        this.learningtoollist = learningtoollist;
    }

    public ArrayList<String> getProgramlanguagelist() {
        return programlanguagelist;
    }

    public void setProgramlanguagelist(ArrayList<String> programlanguagelist) {
        this.programlanguagelist = programlanguagelist;
    }

    @Override
    public String toString() {
        return "YourSelf{" + "fullname=" + fullname + ", address=" + address + ", gender=" + gender + ", age=" + age + ", hobbielist=" + hobbielist + ", learningtoollist=" + learningtoollist + ", programlanguagelist=" + programlanguagelist + '}';
    }
    public void ConvertJSON(JSONObject obj){
       fullname = obj.getString("fullname");
       age = obj.getInt("age");
       gender = obj.getString("gender");
       address = obj.getString("address");
       JSONArray jar = obj.getJSONArray("hobbielist");
        for (int i = 0; i < jar.length(); i++) {
            String str = jar.getString(i);
            hobbielist.add(str);
        }JSONArray jor = obj.getJSONArray("learningtoollist");
        for (int i = 0; i < jor.length(); i++) {
            String str = jor.getString(i);
            learningtoollist.add(str);
        }JSONArray jas = obj.getJSONArray("programlanguagelist");
        for (int i = 0; i < jas.length(); i++) {
            String str = jas.getString(i);
            programlanguagelist.add(str);
        }
    
    }
    
  public void display() {
      System.out.println(toString());
      System.out.println("HobbitsList :");
      for (int i = 0; i < hobbielist.size(); i++) {
          System.out.println("HobbitsList :" +hobbielist.get(i));
      }
      System.out.println("learningtoolList");
      for (int i = 0; i < learningtoollist.size(); i++) {
          System.out.println("LearningtoolList :" +learningtoollist.get(i));
      }
        System.out.println("ProgramlanguageList");
        for (int i = 0; i < programlanguagelist.size(); i++) {
            System.out.println("ProgramlanguageList" +programlanguagelist.get(i));
      }
  }    
    
}


avatar
NguyenHuuThanh [T1907A]
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"

    }
  ]
}
]




avatar
hoangduyminh [T1907A]
2020-04-22 03:29:55




[
  {
    "fullname": "Hoang Duy Minh",
    "age": "18",
    "gender": "Male",
    "address": "Yen Khanh",
    "hobbielist": [
      "football",
      "Game"
    ],
    "learningtoollist": [
      "Laptop",
      "Books",
      "Internet",
    ],
    "programlanguagelist": [
      "Html/css",
      "Java"
    ]
  }
]



/*
 * 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 lesson3;

import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.util.ArrayList;
import java.util.logging.Level;
import java.util.logging.Logger;
import java.lang.reflect.Type;


/**
 *
 * @author Minh
 */
public class Main {
    public static void main(String[] args) {
        FileReader reader = null;
        try {
            reader = new FileReader("YourSelf.json");
            
            Type classOfT = new TypeToken<ArrayList<YourSelf>>(){}.getType();
            
            Gson gson = new Gson();
            ArrayList<YourSelf> list =  gson.fromJson(reader, classOfT);
            
            for (YourSelf yourSelf : list) {
                yourSelf.display();
            }
        } catch (FileNotFoundException ex) {
            Logger.getLogger(Main.class.getName()).log(Level.SEVERE, null, ex);
        }finally{
         if(reader != null){
             try {
                 reader.close();
             } catch (IOException ex) {
                 Logger.getLogger(Main.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 lesson3;

import java.util.ArrayList;
import org.json.JSONArray;
import org.json.JSONObject;

/**
 *
 * @author Minh
 */
public class YourSelf {
    String fullname,address,gender;
    int age;
    ArrayList<String> hobbielist = new ArrayList<>();
    ArrayList<String> learningtoollist = new ArrayList<>();
    ArrayList<String> programlanguagelist = new ArrayList<>();

    public YourSelf() {
    }

    public YourSelf(String fullname, String address, String gender, int age) {
        this.fullname = fullname;
        this.address = address;
        this.gender = gender;
        this.age = age;
    }

    public String getFullname() {
        return fullname;
    }

    public void setFullname(String fullname) {
        this.fullname = fullname;
    }

    public String getAddress() {
        return address;
    }

    public void setAddress(String address) {
        this.address = address;
    }

    public String getGender() {
        return gender;
    }

    public void setGender(String gender) {
        this.gender = gender;
    }

    public int getAge() {
        return age;
    }

    public void setAge(int age) {
        this.age = age;
    }

    public ArrayList<String> getHobbielist() {
        return hobbielist;
    }

    public void setHobbielist(ArrayList<String> hobbielist) {
        this.hobbielist = hobbielist;
    }

    public ArrayList<String> getLearningtoollist() {
        return learningtoollist;
    }

    public void setLearningtoollist(ArrayList<String> learningtoollist) {
        this.learningtoollist = learningtoollist;
    }

    public ArrayList<String> getProgramlanguagelist() {
        return programlanguagelist;
    }

    public void setProgramlanguagelist(ArrayList<String> programlanguagelist) {
        this.programlanguagelist = programlanguagelist;
    }

    @Override
    public String toString() {
        return "YourSelf{" + "fullname=" + fullname + ", address=" + address + ", gender=" + gender + ", age=" + age + ", hobbielist=" + hobbielist + ", learningtoollist=" + learningtoollist + ", programlanguagelist=" + programlanguagelist + '}';
    }
    public void ConvertJSON(JSONObject obj){
       fullname = obj.getString("fullname");
       age = obj.getInt("age");
       gender = obj.getString("gender");
       address = obj.getString("address");
       JSONArray jar = obj.getJSONArray("hobbielist");
        for (int i = 0; i < jar.length(); i++) {
            String str = jar.getString(i);
            hobbielist.add(str);
        }JSONArray jor = obj.getJSONArray("learningtoollist");
        for (int i = 0; i < jor.length(); i++) {
            String str = jor.getString(i);
            learningtoollist.add(str);
        }JSONArray jas = obj.getJSONArray("programlanguagelist");
        for (int i = 0; i < jas.length(); i++) {
            String str = jas.getString(i);
            programlanguagelist.add(str);
        }
    
    }
    
  public void display() {
      System.out.println(toString());
      System.out.println("HobbitsList :");
      for (int i = 0; i < hobbielist.size(); i++) {
          System.out.println("HobbitsList :" +hobbielist.get(i));
      }
      System.out.println("learningtoolList");
      for (int i = 0; i < learningtoollist.size(); i++) {
          System.out.println("LearningtoolList :" +learningtoollist.get(i));
      }
        System.out.println("ProgramlanguageList");
        for (int i = 0; i < programlanguagelist.size(); i++) {
            System.out.println("ProgramlanguageList" +programlanguagelist.get(i));
      }
  }    
    
}


avatar
thienphu [T1907A]
2020-04-21 14:14:15



[
    {
        "fullname": "Do Thien Phu",
        "age": 20,
        "address": "ha noi",
        "likeList": [
            "read book",
            "play soccer",
            "football"
        ],
        "language": [
            "Java",
            "PHP"
        ]

    },
    {
        "fullname": "Nguyen Hong Ngoc",
        "age": 21,
        "address": "ha noi",
        "likeList": [
            "read book",
            "play soccer",
            "football"
        ],
        "language": [
            "Java",
            "PHP"
        ]

    }
]



/*
 * 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 GsonExample3;

import java.util.ArrayList;
import java.util.List;

/**
 *
 * @author Thien Phu
 */
public class Information {

    // đặt tên các biến giống key trong file json thì mới mapp
    private String fullname, address;
    private int age;
    private List<String> likeList;
    private String[] language;

    public Information() {
        likeList = new ArrayList<>();
    }

    public String getFullname() {
        return fullname;
    }

    public void setFullname(String fullname) {
        this.fullname = fullname;
    }

    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<String> getLikeList() {
        return likeList;
    }

    public void setLikeList(List<String> likeList) {
        this.likeList = likeList;
    }

    public String[] getLanguage() {
        return language;
    }

    public void setLanguage(String[] language) {
        this.language = language;
    }
    public void display(){
        System.out.println("Like list: ");
        for (String likeList1 : likeList) {
            System.out.print(likeList1+" , ");
        }
    }

}



/*
 * 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 GsonExample3;

import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.Reader;
import java.lang.reflect.Type;
import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;

/**
 *
 * @author Thien Phu
 */
public class MainInformation {

    public static void main(String[] args) {
        Gson gson = new Gson();
        Reader reader = null;

        String fileurl = "D:\\baitapjava\\BaitapXML\\src\\GsonExample3\\thongtin.json";
        try {
            Type classType = new TypeToken<List<Information>>() {
            }.getType();
            reader = new FileReader(fileurl);
            List<Information> infor = gson.fromJson(reader, classType);

            for (Information information : infor) {
                String jsonString = gson.toJson(information);
                System.out.println(jsonString);
            }

        } catch (FileNotFoundException ex) {
            Logger.getLogger(MainInformation.class.getName()).log(Level.SEVERE, null, ex);
        }
    }
}


avatar
Phí Văn Long [T1907A]
2020-04-21 13:37:16



/*
 * 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 infoGson;

import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.lang.reflect.Type;
import java.util.ArrayList;
import java.util.logging.Level;
import java.util.logging.Logger;

/**
 *
 * @author Nong
 */
public class Main {

    public static void main(String[] args) {
        FileReader reader = null;
        try {
            reader = new FileReader("profile1.json");
            Type classOfT = new TypeToken<ArrayList<Profile>>() {
            }.getType();

            Gson gson = new Gson();
            ArrayList<Profile> list = gson.fromJson(reader, classOfT);
            list.forEach((profile) -> {
                profile.display();
            });
        } catch (FileNotFoundException ex) {
            Logger.getLogger(Main.class.getName()).log(Level.SEVERE, null, ex);
        } finally {
            if (reader != null) {
                try {
                    reader.close();
                } catch (IOException ex) {
                    Logger.getLogger(Main.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 infoGson;

/**
 *
 * @author Nong
 */
public class Profile {

    String fullname, address;
    int age;
    Tools ToolList;

    proLanguage proLanguageList;
    Hobbies HobbiesList;

//    List<Hobbies> HobbiesList;
    public Profile() {
        ToolList = new Tools();
        proLanguageList = new proLanguage();
        HobbiesList = new Hobbies();
//    HobbiesList = new ArrayList<>();

    }

    public String getFullname() {
        return fullname;
    }

    public void setFullname(String fullname) {
        this.fullname = fullname;
    }

    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 Tools getToolList() {
        return ToolList;
    }

    public void setToolList(Tools ToolList) {
        this.ToolList = ToolList;
    }

    public proLanguage getProLanguageList() {
        return proLanguageList;
    }

    public void setProLanguageList(proLanguage proLanguageList) {
        this.proLanguageList = proLanguageList;
    }

    public Hobbies getHobbiesList() {
        return HobbiesList;
    }

    public void setHobbiesList(Hobbies HobbiesList) {
        this.HobbiesList = HobbiesList;
    }

    public void display() {
        System.out.println("Full Name: " + fullname + ", Age: " + age + ", Address: " + address);
        System.out.println(ToolList);
        System.out.println(proLanguageList);
        System.out.println(HobbiesList);

    }

}



/*
 * 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 infoGson;

/**
 *
 * @author Nong
 */
public class Hobbies {

    String hobbies, hobbies1, hobbies2;

    public Hobbies() {

    }

    public String getHobbies() {
        return hobbies;
    }

    public void setHobbies(String hobbies) {
        this.hobbies = hobbies;
    }

    public String getHobbies1() {
        return hobbies1;
    }

    public void setHobbies1(String hobbies1) {
        this.hobbies1 = hobbies1;
    }

    public String getHobbies2() {
        return hobbies2;
    }

    public void setHobbies2(String hobbies2) {
        this.hobbies2 = hobbies2;
    }

    @Override
    public String toString() {
        return "HobbiesList{" + "hobbies=" + hobbies + ", hobbies=" + hobbies1 + ", hobbies=" + hobbies2 + '}';
    }
}



/*
 * 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 infoGson;

/**
 *
 * @author Nong
 */
public class Tools {
    String tool,tool1,tool2;

    public Tools() {
    }

    public String getTool() {
        return tool;
    }

    public void setTool(String tool) {
        this.tool = tool;
    }

    public String getTool1() {
        return tool1;
    }

    public void setTool1(String tool1) {
        this.tool1 = tool1;
    }

    public String getTool2() {
        return tool2;
    }

    public void setTool2(String tool2) {
        this.tool2 = tool2;
    }

    @Override
    public String toString() {
        return "ToolList{" + "tool=" + tool + ", tool=" + tool1 + ", tool=" + tool2 + '}';
    }
    
}



/*
 * 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 infoGson;

/**
 *
 * @author Nong
 */
public class proLanguage {
        String proLanguage,proLanguage1,proLanguage2;

    public proLanguage() {
    }

    public String getProLanguage() {
        return proLanguage;
    }

    public void setProLanguage(String proLanguage) {
        this.proLanguage = proLanguage;
    }

    public String getProLanguage1() {
        return proLanguage1;
    }

    public void setProLanguage1(String proLanguage1) {
        this.proLanguage1 = proLanguage1;
    }

    public String getProLanguge2() {
        return proLanguage2;
    }

    public void setProLanguge2(String proLanguge2) {
        this.proLanguage2 = proLanguge2;
    }

    @Override
    public String toString() {
        return "proLanguageList{" + "proLanguage=" + proLanguage + ", proLanguage=" + proLanguage1 + ", proLanguge=" + proLanguage2 + '}';
    }
        
}


avatar
Minh Nghia [T1907A]
2020-04-20 15:12:08



[
    {

    	"profile_list" : 
    	[
            {
            	"fullname": "Nguyen Minh Nghia",
    	        "age" : 19,
    	        "gender" : "Male",
    	        "address" : "Hai Duong",
                "hobby" : "Soccer",
                "leaningtools" : "Dell",
                "prolanguage" : "PHP"
            }
    	]
    }
]



/*
 * 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 Person_Information_GSON;

/**
 *
 * @author Administrator
 */
public class Profile {
    String fullname, gender, address, hobby, leaningtools,prolanguage;
    int age;

    public Profile() {
    }

    public String getFullname() {
        return fullname;
    }

    public void setFullname(String fullname) {
        this.fullname = fullname;
    }

    public String getGender() {
        return gender;
    }

    public void setGender(String gender) {
        this.gender = gender;
    }

    public String getAddress() {
        return address;
    }

    public void setAddress(String address) {
        this.address = address;
    }

    public String getHobby() {
        return hobby;
    }

    public void setHobby(String hobby) {
        this.hobby = hobby;
    }

    public String getLeaningtools() {
        return leaningtools;
    }

    public void setLeaningtools(String leaningtools) {
        this.leaningtools = leaningtools;
    }

    public String getProlanguage() {
        return prolanguage;
    }

    public void setProlanguage(String prolanguage) {
        this.prolanguage = prolanguage;
    }

    public int getAge() {
        return age;
    }

    public void setAge(int age) {
        this.age = age;
    }

    @Override
    public String toString() {
        return "Profile{" + "fullname=" + fullname + ", gender=" + gender + ", address=" + address + ", hobby=" + hobby + ", leaningtools=" + leaningtools + ", prolanguage=" + prolanguage + ", age=" + age + '}';
    }
    
}



/*
 * 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 Person_Information_GSON;

import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.lang.reflect.Type;
import java.util.ArrayList;
import java.util.logging.Level;
import java.util.logging.Logger;

/**
 *
 * @author Administrator
 */
public class Main {
    public static void main(String[] args) {
        FileReader reader = null;
        
        
        try {
            reader = new FileReader("Profile.json");
            
            Type classOfT = new TypeToken<ArrayList<Profile_Info>>(){}.getType();
            
            Gson gson = new Gson();
            
            ArrayList<Profile_Info> list = gson.fromJson(reader, classOfT);
            
            for (Profile_Info profile_Info : list) {
                profile_Info.display();
            }
        } catch (FileNotFoundException ex) {
            Logger.getLogger(Main.class.getName()).log(Level.SEVERE, null, ex);
        }finally{
            if(reader != null){
                try {
                    reader.close();
                } catch (IOException ex) {
                    Logger.getLogger(Main.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 Person_Information_GSON;

import com.google.gson.annotations.SerializedName;
import java.util.ArrayList;
import java.util.List;

/**
 *
 * @author Administrator
 */
public class Profile_Info {
    @SerializedName("profile_list")
    List<Profile> profileList;

    public Profile_Info() {
        profileList = new ArrayList<>();
    }

    public List<Profile> getProfileList() {
        return profileList;
    }

    public void setProfileList(List<Profile> profileList) {
        this.profileList = profileList;
    }

    public void display(){
        for (Profile profile : profileList) {
            System.out.println(profile);
        }
    }
    
}


avatar
Đường Thanh Bình [T1907A]
2020-04-20 13:40:03



/*
 * 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 T1907AJSON;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.util.ArrayList;
import java.util.logging.Level;
import java.util.logging.Logger;
import org.json.JSONArray;
import org.json.JSONObject;

/**
 *
 * @author Administrator
 */
public class Main {
    public static void main(String[] agrs) throws IOException{
        FileInputStream fis = null;
        try {
            fis = new FileInputStream("infor.json");
            
            StringBuilder strB = new StringBuilder();
            int code;
            while ((code = fis.read())!=-1) {                
                strB.append((char) code);
            }
            String Contend = strB.toString();
            
            
            JSONArray array = new JSONArray(Contend);
            
            ArrayList<infor> inforList = new ArrayList<>();
            
            for (int i = 0; i < array.length(); i++) {
                JSONObject obj = array.getJSONObject(i);
                infor infor = new infor();
                infor.ConvertJSON(obj);
                inforList.add(infor);
            }
            for (infor object : inforList) {
                object.display();
            }
            
        } catch (FileNotFoundException ex) {
            Logger.getLogger(Main.class.getName()).log(Level.SEVERE, null, ex);
        } catch (IOException ex) {
            Logger.getLogger(Main.class.getName()).log(Level.SEVERE, null, ex);
        }finally{
            if (fis != null) {
                try {
                    fis.close();
                } catch (IOException ex) {
                    Logger.getLogger(Main.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 T1907AJSON;
import org.json.JSONObject;
/**
 *
 * @author Administrator
 */
public class infor {
    String fullname,address,gender,status,learningtool,programminglanguage;
    int age;

    public infor() {
    }

    public infor(String fullname, String address, String gender, String status, String learningtool, String programminglanguage, int age) {
        this.fullname = fullname;
        this.address = address;
        this.gender = gender;
        this.status = status;
        this.learningtool = learningtool;
        this.programminglanguage = programminglanguage;
        this.age = age;
    }

    public String getFullname() {
        return fullname;
    }

    public void setFullname(String fullname) {
        this.fullname = fullname;
    }

    public String getAddress() {
        return address;
    }

    public void setAddress(String address) {
        this.address = address;
    }

    public String getGender() {
        return gender;
    }

    public void setGender(String gender) {
        this.gender = gender;
    }

    public String getStatus() {
        return status;
    }

    public void setStatus(String status) {
        this.status = status;
    }

    public String getLearningtool() {
        return learningtool;
    }

    public void setLearningtool(String learningtool) {
        this.learningtool = learningtool;
    }

    public String getProgramminglanguage() {
        return programminglanguage;
    }

    public void setProgramminglanguage(String programminglanguage) {
        this.programminglanguage = programminglanguage;
    }

    public int getAge() {
        return age;
    }

    public void setAge(int age) {
        this.age = age;
    }
    
    public void convertJSON(JSONObject obj){
        fullname = obj.getString("fullname");
        age = obj.getInt("age");
        address = obj.getString("address");
        gender = obj.getString("gender");
        status = obj.getString("status");
        learningtool = obj.getString("learningtool");
        programminglanguage = obj.getString("programminglanguage");
    }

    @Override
    public String toString() {
        return "infor{" + "fullname=" + fullname + ", address=" + address + ", gender=" + gender + ", status=" + status + ", learningtool=" + learningtool + ", programminglanguage=" + programminglanguage + ", age=" + age + '}';
    }

}



{
	"infor": {
		"fullname": "Duong Thanh Binh",
		"age": 18,
		"address": "Ha Noi",
		"gender": "male",
		"status": "single",
		"learningtool": "laptop",
		"programminglanguage": "java"
	}
}


avatar
nguyễn văn huy [T1907A]
2020-04-20 09:49:02



/*
 * 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 JSon;

import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.lang.reflect.Type;
import java.util.ArrayList;
import java.util.logging.Level;
import java.util.logging.Logger;

/**
 *
 * @author ASUS
 */
public class Main {
    public static void main(String[] args) {
        FileReader reader = null;
        
        try {
            //gson java example => vi du trong tung truong hop 1
            reader = new FileReader("student.json");
            
            Type OfT;
            OfT = new TypeToken<ArrayList<Information>>(){}.getType();
            
            Gson gson = new Gson();
            ArrayList<Information> thaolist = gson.fromJson(reader, OfT);
            
            for (Information thaou : thaolist) {
                thaou.display();
            }
        } catch (FileNotFoundException ex) {
            Logger.getLogger(Main.class.getName()).log(Level.SEVERE, null, ex);
        } finally {
            if(reader != null) {
                try {
                    reader.close();
                } catch (IOException ex) {
                    Logger.getLogger(Main.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 JSon;

import org.json.JSONObject;

/**
 *
 * @author ASUS
 */
public class Interests {
    String Movie,Music,code;

    public Interests() {
    }

    public Interests(String Movie, String Music, String code) {
        this.Movie = Movie;
        this.Music = Music;
        this.code = code;
    }

    public String getMovie() {
        return Movie;
    }

    public void setMovie(String Movie) {
        this.Movie = Movie;
    }

    public String getMusic() {
        return Music;
    }

    public void setMusic(String Music) {
        this.Music = Music;
    }

    public String getCode() {
        return code;
    }

    public void setCode(String code) {
        this.code = code;
    }

    @Override
    public String toString() {
        return "Interests{" + "Movie=" + Movie + ", Music=" + Music + ", code=" + code + '}';
    }

    void convertJSON(JSONObject InterestsObject) {
        throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
    }
    
}
>>>>
/*
 * 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 JSon;

import org.json.JSONObject;

/**
 *
 * @author ASUS
 */
public class Information {
    String name,address,tool,language;
    int age;
    Interests thao;

    public Information() {
        thao=new Interests();
    }

    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 String getTool() {
        return tool;
    }

    public void setTool(String tool) {
        this.tool = tool;
    }

    public String getLanguage() {
        return language;
    }

    public void setLanguage(String language) {
        this.language = language;
    }

    public int getAge() {
        return age;
    }

    public void setAge(int age) {
        this.age = age;
    }

    public Interests getThao() {
        return thao;
    }

    public void setThao(Interests thao) {
        this.thao = thao;
    }
    public void converJSon(JSONObject obj){
        name=obj.getString("name");
        address=obj.getString("address");
        age=obj.getInt("age");
        tool=obj.getString("tool");
        language=obj.getString("language");
        JSONObject InterestsObject=obj.getJSONObject("Interests");
        thao.convertJSON(InterestsObject);
    }
    public void display(){
        System.out.println("name:"+name+"age"+age+"address"+address+"tool"+tool+"language"+language);
        System.out.println(thao);
                
    }
}
>>>>>
[
	{
		"name": "Nguyen Van Huy",
		"address": "Thai Binh",
		"age":"18",
		"tool":"computer",
		"language":"java",
		"Interests": {
			"Movie":"watch movie",
			"Music":"listen music",
			"code":"code",
			"code":"code",
		},

	}
]


avatar
Trương Công Vinh [T1907A]
2020-04-20 09:27:30



/*
 * 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 Profile;

import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.lang.reflect.Type;
import java.util.ArrayList;
import java.util.logging.Level;
import java.util.logging.Logger;

/**
 *
 * @author DELL
 */
public class Main_gson {
    public static void main(String[] args) {
        FileReader reader = null;
        
        try {
            reader = new FileReader("d://JSON/profile.json");
            
            Type classOfT = new TypeToken<ArrayList<person>>(){}.getType();
            
            Gson gson = new Gson();
            ArrayList<person> list = gson.fromJson(reader, classOfT);
            for (person object : list) {
                object.display();
            }
        } catch (FileNotFoundException ex) {
            Logger.getLogger(Main_gson.class.getName()).log(Level.SEVERE, null, ex);
        }finally{
            if (reader!=null) {
                try {
                    reader.close();
                } catch (IOException ex) {
                    Logger.getLogger(Main_gson.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 Profile;

import com.google.gson.annotations.SerializedName;
import java.util.ArrayList;
import org.json.JSONArray;
import org.json.JSONObject;
import org.json.JSONString;

/**
 *
 * @author DELL
 */
public class person {
     String fullname,gender, address , email ;
    int age;
    @SerializedName("favourite_list")
    ArrayList<String> favourite ;
    @SerializedName("learningtools_list")
    ArrayList<String> learningtool ;
    @SerializedName("programming_languages_you_like")
    ArrayList<String> programming_language ;

    public person() {
        favourite = new ArrayList<>();
        learningtool = new ArrayList<>();
        programming_language = new ArrayList<>();
    }

    public person(String fullname, String gender, String address, String email, int age) {
        this.fullname = fullname;
        this.gender = gender;
        this.address = address;
        this.email = email;
        this.age = age;
        
    }

    public String getFullname() {
        return fullname;
    }

    public void setFullname(String fullname) {
        this.fullname = fullname;
    }

    public String getGender() {
        return gender;
    }

    public void setGender(String gender) {
        this.gender = gender;
    }

    public String getAddress() {
        return address;
    }

    public void setAddress(String address) {
        this.address = address;
    }

    public String getEmail() {
        return email;
    }

    public void setEmail(String email) {
        this.email = email;
    }

    public int getAge() {
        return age;
    }

    public void setAge(int age) {
        this.age = age;
    }

   

    public ArrayList<String> getFavourite() {
        return favourite;
    }

    public void setFavourite(ArrayList<String> favourite) {
        this.favourite = favourite;
    }

    public ArrayList<String> getLearningtool() {
        return learningtool;
    }

    public void setLearningtool(ArrayList<String> learningtool) {
        this.learningtool = learningtool;
    }

    public ArrayList<String> getProgramming_language() {
        return programming_language;
    }

    public void setProgramming_language(ArrayList<String> programming_language) {
        this.programming_language = programming_language;
    }

    @Override
    public String toString() {
        return "person{" + "fullname=" + fullname + ", gender=" + gender + ", address=" + address + ", email=" + email + ", age=" + age + '}';
    }
    public void ConvertJSON(JSONObject obj){
        fullname = obj.getString("fullname");
        gender = obj.getString("gender");
        address = obj.getString("address");
        email = obj.getString("email");
        age = obj.getInt("age");
        JSONArray arr = obj.getJSONArray("favourite_list");
        for (int i = 0; i < arr.length(); i++) {
            String str = arr.getString(i);
            favourite.add(str);
        }
        JSONArray arr1 = obj.getJSONArray("learningtools_list");
        for (int i = 0; i < arr.length(); i++) {
            String str = arr.getString(i);
            learningtool.add(str);
        }
        JSONArray arr2 = obj.getJSONArray("programming_languages_you_like");
        for (int i = 0; i < arr.length(); i++) {
            String str = arr.getString(i);
            programming_language.add(str);
        }
    }

    public void display(){
        System.out.println(toString());
        System.out.println("Favourites :");
        for (int i = 0; i < favourite.size(); i++) {
            System.out.println("Favourite : "+favourite.get(i));
        }
        System.out.println("learningtools_list");
        for (int i = 0; i < learningtool.size(); i++) {
            System.out.println("tool : " + learningtool.get(i) );
        }
        System.out.println("programming_languages_you_like : ");
        for (int i = 0; i < programming_language.size(); i++) {
            System.out.println("programming_language : "+programming_language.get(i));
        }
    }
}



[
  {
    "fullname": "Vinh",
    "age": "18",
    "gender": "Male",
    "email": "duongcongvinh18122001@gmail.com",
    "address": "Earth",
    "favourite_list": [
      "coding",
      "coding"
    ],
    "learningtools_list": [
      "tool 0",
      "tool 1",
      "tool 3",
      "tool 2"
    ],
    "programming_languages_you_like": [
      "@@",
      "@@"
    ]
  }
]