[Video] Gson Trong Java - Bài Tập Quản Lý Sản Phẩm + Json Trong Java

[Share Code] gson trong Java - Vi dụ json trong java - Bài tập quản lý sản phẩm + json trong Java

JSON (Mini Project) - Chuyển Array/Object -> json trong Java - Chuyển json từ file -> Array/Object trong java - API (JSON) -> Thao tác bằng Java. ================================================= Mini Project: Viết 1 chương trình quản lý sản phẩm (Kho hang) 1. Nhập thông tin sản phẩm 2. Hiển thị thông tin sản phẩm 3. Lưu File 4. Đọc nội dung từ File 5. Thoát chương trình Phân tích: Product: - Title - thumbnail - price - description

Tạo đối tượng sản phẩm (product)

/* * 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 aptech.json.lesson01; import java.util.Scanner; /** * * @author Diep.Tran */ public class Product { String title; String thumbnail; String description; float price; public Product() { } public Product(String title, String thumbnail, String description, float price) { this.title = title; this.thumbnail = thumbnail; this.description = description; this.price = price; } public String getTitle() { return title; } public void setTitle(String title) { this.title = title; } public String getThumbnail() { return thumbnail; } public void setThumbnail(String thumbnail) { this.thumbnail = thumbnail; } public String getDescription() { return description; } public void setDescription(String description) { this.description = description; } public float getPrice() { return price; } public void setPrice(float price) { this.price = price; } @Override public String toString() { return "Product{" + "title=" + title + ", thumbnail=" + thumbnail + ", description=" + description + ", price=" + price + '}'; } public void input() { System.out.println("=== NHAP THONG TIN SAN PHAM ==="); Scanner scan = new Scanner(System.in); System.out.println("Nhap tieu de: "); title = scan.nextLine(); System.out.println("Nhap thumbnail: "); thumbnail = scan.nextLine(); System.out.println("Nhap mo ta san pham: "); description = scan.nextLine(); System.out.println("Gia tien: "); price = Float.parseFloat(scan.nextLine()); } }

Chương trình

/* * 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 aptech.json.lesson01; import com.google.gson.Gson; import com.google.gson.reflect.TypeToken; import java.io.File; import java.io.FileNotFoundException; import java.io.FileOutputStream; import java.io.FileReader; import java.io.IOException; import java.io.UnsupportedEncodingException; import java.lang.reflect.Type; import java.util.ArrayList; import java.util.List; import java.util.Scanner; import java.util.logging.Level; import java.util.logging.Logger; /** * * @author Diep.Tran */ public class Main { static List<Product> dataList = new ArrayList<>(); static Scanner scan = new Scanner(System.in); public static void main(String[] args) { int choose; do { showMenu(); choose = Integer.parseInt(scan.nextLine()); switch(choose) { case 1: input(); break; case 2: display(); break; case 3: saveFile(); break; case 4: readFile(); break; case 5: readAPI(); break; case 6: System.out.println("Thoat chuong trinh!!!"); break; default: System.out.println("Nhap sai!!!"); break; } } while(choose != 6); } static void showMenu() { System.out.println("1. Nhap thong tin san pham"); System.out.println("2. Hien thi thong tin san pham"); System.out.println("3. Luu file product.json"); System.out.println("4. Doc noi dung file product.json"); System.out.println("5. Thao tac API (fake)"); System.out.println("6. Thoat"); System.out.println("Chon: "); } private static void input() { System.out.println("Nhap so san pham can them (N): "); int n = Integer.parseInt(scan.nextLine()); for (int i = 0; i < n; i++) { Product product = new Product(); product.input(); dataList.add(product); } } private static void display() { System.out.println("=== THONG TIN SAN PHAM ==="); dataList.forEach((product) -> { System.out.println(product); }); } private static void saveFile() { //Buoc 1. Convert dataList (Array) -> json string Gson gson = new Gson(); String json = gson.toJson(dataList); System.out.println("JSON: " + json); //Save du lieu vao file product.json FileOutputStream fos = null; try { fos = new FileOutputStream("product.json"); byte[] data = json.getBytes("utf8"); fos.write(data); } catch (FileNotFoundException ex) { Logger.getLogger(Main.class.getName()).log(Level.SEVERE, null, ex); } catch (UnsupportedEncodingException 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(fos != null) { try { fos.close(); } catch (IOException ex) { Logger.getLogger(Main.class.getName()).log(Level.SEVERE, null, ex); } } } } private static void readFile() { FileReader reader = null; try { File file = new File("product.json"); reader = new FileReader(file); Gson gson = new Gson(); Type type = new TypeToken<List<Product>>(){}.getType(); dataList = gson.fromJson(reader, type); display(); } catch (FileNotFoundException ex) { Logger.getLogger(Main.class.getName()).log(Level.SEVERE, null, ex); } finally { try { reader.close(); } catch (IOException ex) { Logger.getLogger(Main.class.getName()).log(Level.SEVERE, null, ex); } } } private static void readAPI() { } }

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