首页 > 编程语言 >java:Json

java:Json

时间:2023-11-16 21:55:52浏览次数:43  
标签:java name return Json user siblings public occupation

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

相关文章

  • Java IO
    IO,即in和out,也就是输入和输出,指应用程序和外部设备之间的数据传递,常见的外部设备包括文件、管道、网络连接。Java中是通过流处理IO的,那么什么是流?流(Stream),是一个抽象的概念,是指一连串的数据(字符或字节),是以先进先出的方式发送信息的通道。当程序需要读取数据的时候,就会开启一个通......
  • Java常用类
    在学习使用Java语言时,我们常常要调用各种方法,而每个类里都有对应的方法,接下来介绍Java中一些常用类:- 1.String类eg.抽取身份证中的出生日期:Stringl="340822194510016411";System.out.print("出生日期是:");Sys......
  • Java数组03:三种初始化及内存分析
    声明的时候数组并不存在,只有创建的时候数组才存在  publicclassArrayDemo02{publicstaticvoidmain(String[]args){//静态初始化:创建+赋值int[]a={1,2,3,4,5,6,7,8};System.out.println(a[0]);//动态初始......
  • Java数组02:数组的声明和创建
    ublicclassArrayDemo01{publicstaticvoidmain(String[]args){//数组类型int[]nums;//intnums[];声明一个数组nums=newint[10];//这里面可以存放10个int类型的数字;创建一个数组//给数组赋值for(inti=0;i<=9;+......
  • JavaWeb--响应字符&字节数据
    Response响应字符数据 //text/html解码html,charset解码汉字response.setContentType("text/html;charset=utf-8");//1、获取字符输入流PrintWriterwriter=response.getWriter();writer.write("你好");writer.write("<h1>124</h1>");响应字节数据添加一个i......
  • 11.16 基本完成个人任务管理系统项目后重新复习JavaScript高级程序设计——声明var与l
    我看的是js高级程序设计第四版,前两章快速了解了一下,第三章开始慢啃,虽然内容枯燥,很多东西自己也知道了,但还是有一些收获的。比如,声明变量的三个关键词:var、let、const;var以前经常用但是会出问题,相比let没有那么严谨(var声明范围函数作用域,而let声明范围块级作用域)。看个例子:这是v......
  • Java方法06:递归讲解
     publicclassDemo05{publicstaticvoidmain(String[]args){//打印5的阶乘System.out.println(f(5));}publicstaticintf(intn){if(n==1){return1;}else{returnn*f(n-1);......
  • Java方法07:练习打一个计算器
    importjava.util.Scanner;publicclassDemo06{publicstaticvoidmain(String[]args){Scannerscanner=newScanner(System.in);Stringy="Y";while(y.equals("Y")){System.out.println(&quo......
  • java的简单绘图
    java简单绘图importjavax.swing.*;importjava.awt.*;publicclassMainextendsJPanel{privatestaticfinalintN=10;privatestaticfinaldoubleP=0.34;@OverrideprotectedvoidpaintComponent(Graphicsg){super.paintComponent(g);Graphics2D......
  • java的数据类型介绍
    1.js的数据类型分类基本数据类型:数值类型[整数、浮点型] ,布尔型(true,false),字符串2.复合数据类型:数组,对象3.特殊数据类型:nullundefined//以ox开头,表示一个16进制的数0x111//1+1*16+1*16*16//0开头,表示8进制//浮点数的写法4e5=4*10的5次方//布尔varb1=truevarb2......