/** * 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(); } }
标签:java,name,return,Json,user,siblings,public,occupation From: https://www.cnblogs.com/geovindu/p/17837364.html