JSON转换(个人随记一)01

UserInfo类

package com.kiki.pojo;

import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
import lombok.ToString;

@Data
@AllArgsConstructor
@NoArgsConstructor
@ToString
public class UserInfo {

    private String name;

    private Integer age;

    private String addr;

    private String sex;
}

测试类

package com.kiki.test;


import com.alibaba.fastjson.JSON;
import com.kiki.pojo.UserInfo;

public class KikiTest01 {

    public static void main(String[] args) {

        String jsonString="{name:'kiki01',age:18,addr:'beijing',sex:'male'}";
        UserInfo userInfo = new UserInfo("kiki02", 24, "xian", "male");
        System.out.println(userInfo);
        System.out.println("===========================================");
        UserInfo userInfo1 = JSON.parseObject(jsonString, UserInfo.class);
        System.out.println(userInfo1);
        System.out.println("===========================================");
        String jsonString1 = JSON.toJSONString(userInfo1);
        System.out.println(jsonString1);
        Long num = 1234L;
        String s = num.toString();
        System.out.println(s);
        System.out.println(num);
    }
}
/*
输出结果:
UserInfo(name=kiki02, age=24, addr=xian, sex=male)
===========================================
UserInfo(name=kiki01, age=18, addr=beijing, sex=male)
===========================================
{"addr":"beijing","age":18,"name":"kiki01","sex":"male"}
1234
1234
*/

map相关随记

package com.kiki.pojo;

import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
import lombok.ToString;

import java.util.HashMap;
import java.util.Iterator;
import java.util.Set;

public class MyTest {
    public static void main(String[] args) {

        HashMap<Phone,String> map = new HashMap<Phone, String>();
        map.put(new Phone("Apple",7000),"美国");
        map.put(new Phone("HuaWei",3299),"中国");
        map.put(new Phone("Sony",5000),"日本");
        Set<Phone> phones = map.keySet();
        System.out.println(phones);
        System.out.println("======================");
        Iterator<Phone> iterator = phones.iterator();
        System.out.println(iterator);
        System.out.println("======================");
        while (iterator.hasNext()) {
            Phone next = iterator.next();
            System.out.println(next);
            System.out.println("===================");
            //String s = map.get(next);
            System.out.println(next.getBrand()+"=="+next.getPrice()+"=="+map.get(next));
        }


    }
}
/*输出结果:
[Phone(Brand=Apple, Price=7000), Phone(Brand=HuaWei, Price=3299), Phone(Brand=Sony, Price=5000)]
======================
java.util.HashMap$KeyIterator@677327b6
======================
Phone(Brand=Apple, Price=7000)
===================
Apple==7000==美国
Phone(Brand=HuaWei, Price=3299)
===================
HuaWei==3299==中国
Phone(Brand=Sony, Price=5000)
===================
Sony==5000==日本
*/




@Data
@AllArgsConstructor
@NoArgsConstructor
@ToString
class Phone{

    private String Brand;//品牌
    private int Price;
}

pom.xml

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>

    <groupId>com.kiki</groupId>
    <artifactId>kiki-code</artifactId>
    <version>1.0-SNAPSHOT</version>


    <dependencies>
        <dependency>
            <groupId>org.projectlombok</groupId>
            <artifactId>lombok</artifactId>
            <version>1.18.12</version>
        </dependency>

        <dependency>
            <groupId>org.junit</groupId>
            <artifactId>junit-bom</artifactId>
            <version>5.6.2</version>
        </dependency>

        <dependency>
            <groupId>com.alibaba</groupId>
            <artifactId>fastjson</artifactId>
            <version>1.2.70</version>
        </dependency>
    </dependencies>
</project>
posted @ 2020-08-04 17:37  木木夕人可  阅读(15)  评论(0编辑  收藏  举报