Mục Lục
Mục Lục
Exception & Collection Video Tìm hiểu exception + debug + package - lập trình Java Nâng Cao Video Tìm hiểu collections, Sets, Maps và Generic - lập trình java nâng cao Chương trình quản lý sinh viên bằng HashMap - Java Advanced Bài tập ôn luyện String, StringBuilder, StringBuffer - Java Advanced Bài tập Exception trong Java File Video Hướng dẫn đọc ghi file FileInputStream, FileOutputStream, ObjectInputStream trong java - lập trình java nâng cao File - Quản lý thông tin sinh viên Quản lý thông tin sinh & lưu object file - Java nâng cao Video Quản lý thông tin sinh & lưu object file - Java nâng cao Video Bài tập quản lý bán vé máy bay - java nâng cao Thread Video Tìm hiểu Thread trong Java - Phần 2 - lập trình java nâng cao Thread thực hiện in ngẫu nhiên các số nguyên và ký tự Video Thread thực hiện in ngẫu nhiên các số nguyên và ký tự - Phần 1 Video Thread thực hiện in ngẫu nhiên các số nguyên và ký tự & Đông bộ thread + wait notify + synchronized + Java nâng cao - Phần 2 Sử dụng MultiThreading sinh ký tự a-zA-Z trong java CSDL Video Kết nối CSDL bằng java swing phần 1 - lập trình java nâng cao Design Pattern & Đa Ngôn Ngữ Video Hướng dẫn tìm hiểu đệ quy qua bài Fibonaci - Recursion Fibonaci - Java Phân chia mảng số nguyên thành 2 phần + chắc + lẻ Video Phân chia mảng số nguyên thành 2 phần + chắc + lẻ Video Design Pattern lập trình java nâng cao - lập trình java nâng cao Bài tập - Chương trình quản lý sách - lập trình đa ngôn ngữ - Khoá học lập trình Java nâng cao XML & JSON & CSDL Video Tìm hiểu XML & Hướng dẫn phân tích tài liệu XML quản lý lớp học bằng Java XML: Viết chương trình phân tích tài liệu XML thông tin cá nhân bằng Java Video - Hướng dẫn tạo dự án quản lý sinh viên + import + export XML File XML: Java Swing|FX Quản lý thông tin cá nhân Profile bằng java - import + export XML File Video Json/Gson & Java - Phân tích dữ liệu lớp học JSON bằng Java -Ứng dụng quản lý lớp học Java nâng cao Video gson trong Java - Vi dụ json trong java - Bài tập quản lý sản phẩm + json trong Java JSON: Tạo JSON file & đọc nội dung JSON file thông tin cá nhân bằng Java Bài tập - Nhập thông tin sinh viên từ JSON vào CSDL - Lập trình Java nâng cao. Mini Project - Phân tích dự án + Phát triển phần mềm ATM Chương trình quản lý sinh viên Java + Import/Export JSON + File - Lập Trình Java Video C1 > Chương trình quản lý sinh viên Java + Import/Export JSON + File - Lập Trình Java Video C2 > Chương trình quản lý sinh viên Java + Import/Export JSON + File - Lập Trình Java Bài tập - Viết chương trình quản lý tiêm chủng Vacxin COVID-19 - Lập trình Java nâng cao. Bài tập - Viết tools hỗ trợ đọc logs bằng Java Bài tập - Viết chương trình quản lý sinh viên XML - JSON - Lập trình Java Quản lý sinh viên + XML + JSON + MySQL bằng Java Video Quản lý sinh viên + XML + JSON + MySQL bằng Java Ôn Tập Tổng Quát & Exmination Bài tập luyện thi Java2 Bài thi Java 2
Java Advanced

[Video] gson trong Java - Vi dụ json 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() {
    }
}




Đăng nhập để làm bài kiểm tra

Chưa có kết quả nào trước đó

×