java:Json

/**
 * encoding: utf-8
 * 版權所有 2023 塗聚文有限公司
 * 許可信息查看:
 * 描述:
 * # Author    : geovindu,Geovin Du 塗聚文.
 * # IDE       : IntelliJ IDEA 2023.1 Java 17
 * # Datetime  : 2023 - 2023/11/16 - 12:29
 * # User      : geovindu
 * # Product   : IntelliJ IDEA
 * # Project   : javademo
 * # File      : User.java  類
 * # explain   : 學習
 **/

package Model;

import java.util.Objects;


/**
 * 用戶
 * @author geovindu
 *
 */
public class User {


    /**
     *姓名
     */
    private String name;
    /**
     *職業
     */
    private String occupation;
    /**
     *兄弟姐妹有幾人
     */
    private int siblings;
    /**
     *身高
     */
    private double height;
    /**
     *結婚否
     */
    private boolean married;

    /**
     *
     * @param name
     * @param occupation
     * @param siblings
     * @param height
     * @param married
     */
    public User(String name, String occupation, int siblings,
                double height, boolean married) {

        this.name = name;
        this.occupation = occupation;
        this.siblings = siblings;
        this.height = height;
        this.married = married;
    }

    /**
     *獲取姓名
     * @return
     */
    public String getName() {
        return name;
    }

    /**
     *設置姓名
     * @param name
     */
    public void setName(String name) {
        this.name = name;
    }

    /**
     *
     * @return
     */
    public String getOccupation() {
        return occupation;
    }

    /**
     *
     * @param occupation
     */
    public void setOccupation(String occupation) {
        this.occupation = occupation;
    }

    /**
     *
     * @return
     */
    public int getSiblings() {
        return siblings;
    }

    /**
     *
     * @param siblings
     */
    public void setSiblings(int siblings) {
        this.siblings = siblings;
    }

    /**
     *
     * @return
     */
    public double getHeight() {
        return height;
    }

    /**
     *
     * @param height
     */
    public void setHeight(double height) {
        this.height = height;
    }

    /**
     *
     * @return
     */
    public boolean isMarried() {
        return married;
    }

    /**
     *
     * @param married
     */
    public void setMarried(boolean married) {
        this.married = married;
    }

    /**
     *
     * @param o
     * @return
     */
    @Override
    public boolean equals(Object o) {
        if (this == o) return true;
        if (o == null || getClass() != o.getClass()) return false;
        User user = (User) o;
        return siblings == user.siblings && Double.compare(user.height, height) == 0
                && married == user.married && Objects.equals(name, user.name)
                && Objects.equals(occupation, user.occupation);
    }

    /**
     *
     * @return
     */
    @Override
    public int hashCode() {
        return Objects.hash(name, occupation, siblings, height, married);
    }

    /**
     *
     * @return
     */
    @Override
    public String toString() {
        final StringBuilder sb = new StringBuilder("User{");
        sb.append("name='").append(name).append('\'');
        sb.append(", occupation='").append(occupation).append('\'');
        sb.append(", siblings=").append(siblings);
        sb.append(", height=").append(height);
        sb.append(", married=").append(married);
        sb.append('}');
        return sb.toString();
    }

}

  

/**
 * encoding: utf-8
 * 版權所有 2023 塗聚文有限公司
 * 許可信息查看:
 * 描述:
 * # Author    : geovindu,Geovin Du 塗聚文.
 * # IDE       : IntelliJ IDEA 2023.1 Java 17
 * # Datetime  : 2023 - 2023/11/16 - 21:24
 * # User      : geovindu
 * # Product   : IntelliJ IDEA
 * # Project   : javademo
 * # File      : User.java  類
 * # explain   : 學習
 **/

package BLL;

import org.json.JSONObject;
import org.json.JSONArray;
import org.json.JSONObject;
import org.json.JSONWriter;
import org.json.CDL;
import java.io.IOException;
import java.nio.charset.StandardCharsets;
import java.nio.file.Files;
import java.nio.file.Paths;


public class UserBll {

    /**
     *
     */
    public  static void getUser()
    {

        var user = new Model.User("John Doe", "gardener",
                2, 172.35, true);

        var userjo = new JSONObject(user);

        System.out.println(userjo);

        System.out.println(userjo.get("name"));
        System.out.println(userjo.get("occupation"));
        System.out.println(userjo.get("siblings"));
    }

    /**
     *
     */
    public  static void getUserArry()
    {
        var user = new JSONObject();

        user.put("name", "John Doe");
        user.put("occupation", "gardener");
        user.put("siblings", Integer.valueOf(2));
        user.put("height", Double.valueOf(172.35));
        user.put("married", Boolean.TRUE);

        var cols = new JSONArray();
        cols.put("red");
        cols.put("blue");
        cols.put("navy");

        user.put("favCols", cols);

        var userJson = user.toString();

        System.out.println(userJson);

    }

    /**
     *
     */
    public static void getWrite()
    {

        var user = new StringBuilder();

        var writer = new JSONWriter(user);

        writer.object();
        writer.key("name").value("John Doe");
        writer.key("occupation").value("gardener");
        writer.key("siblings").value(2);
        writer.key("married").value(true);

        writer.key("favCols");
        writer.array();
        writer.value("red");
        writer.value("blue");
        writer.value("navy");
        writer.endArray();

        writer.endObject();

        System.out.println(user);
    }

    /**
     *
     */
    public static  void getCsvtoJson()
    {
        try {
            var data = Files.readString(Paths.get("src/resources/data.csv"),
                    StandardCharsets.UTF_8);

            JSONArray usersJson = CDL.toJSONArray(data);
            usersJson.forEach(System.out::println);
        }
        catch (Exception exception)
        {
            System.out.println(exception.getMessage());
        }
    }

}

  

調用:

/**
 * encoding: utf-8
 * 版權所有 2023 塗聚文有限公司
 * 許可信息查看:
 * 描述:
 * # Author    : geovindu,Geovin Du 塗聚文.
 * # IDE       : IntelliJ IDEA 2023.1 Java 17
 * # Datetime  : 2023 - 2023/11/15 - 6:02
 * # User      : geovindu
 * # Product   : IntelliJ IDEA
 * # Project   : javademo
 * # File      : main.java  類
 * # explain   : 學習
 *GraalVM
 * https://www.graalvm.org/
 * https://www.oracle.com/cn/java/graalvm/
 * https://www.graalvm.org/downloads/#
 * com.darwinsys
 *CVS,Subversion,Git
 * http://git-scm.com
 */

import com.darwinsys.util.*;
import Common.Env;
import Common.JsonHelper;
import BLL.UserBll;

public class Main {

    /**
     *
     * @param args
     */
    public static void main(String[] args)
    {
        System.out.println("Hello java language world! 塗聚文!");

        UserBll bll=new UserBll();
        UserBll.getUser();
        UserBll.getUserArry();
        UserBll.getWrite();
        UserBll.getCsvtoJson();

        System.out.println(Env.getEnv());
        System.out.println(Env.getColor());
        Env.getEnvList();

    }
}

  

 

 

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章