alibaba.fastjson.JSONObject中JSON转对象,对象中有其他对象属性的parseObject转换

alibaba.fastjson.JSONObject中JSON转对象,对象中有其他对象属性

alibaba.fastjson.JSONObject中JSON转对象,对象中有其他对象属性时,满足以下几个条件也能使用JSONObject.parseObject()转换
1. 子对象的对象名,要和JSON中的字段名一致,否则会爆NPE
2. JSON串中的子对象字段需要在使用大括号{}包裹

一个简单的DEMO

import com.alibaba.fastjson.JSONObject;

public class Test {
    public static class One {
        private int id;
        private int age;
        private Two two;

        public int getId() {
            return id;
        }

        public void setId(int id) {
            this.id = id;
        }

        public int getAge() {
            return age;
        }

        public void setAge(int age) {
            this.age = age;
        }

        public Two getTwo() {
            return two;
        }

        public void setTwo(Two two) {
            this.two = two;
        }

        @Override
        public String toString() {
            return "One{" +
                    "id=" + id +
                    ", age=" + age +
                    ", two=" + two +
                    '}';
        }
    }

    public static class Two {
        private String name;
        private int height;

        public String getName() {
            return name;
        }

        public void setName(String name) {
            this.name = name;
        }

        public int getHeight() {
            return height;
        }

        public void setHeight(int height) {
            this.height = height;
        }

        @Override
        public String toString() {
            return "Two{" +
                    "name='" + name + '\'' +
                    ", height=" + height +
                    '}';
        }
    }
    public static void main(String[] args) {
        String ret = "{\"id\": 1,\n" +
                "  \"age\": 25,\n" +
                "  \"two\": {\n" +
                "    \"name\": \"John\",\n" +
                "    \"height\": 180\n" +
                "  }}";
        One one = JSONObject.parseObject(ret,  One.class);
        Two two = one.getTwo();
        System.out.println(one.toString());
        System.out.println(two.toString());

    }
}

ret

One{id=1, age=25, two=Two{name='John', height=180}}
Two{name='John', height=180}

DEMO说明

{
  "id": 1,
  "age": 25,
  "two": {
    "name": "John",
    "height": 180
  }
}

JSON串的格式,对应了子对象two,并将子对象的属性使用{}

alibaba.fastjson.JSONObject下的方法是需要实体类有getter、setter方法的,否则会将属性设为默认值。

把getter setter方法删除得到结果如下:

One{id=0, age=0, two=null}

fastjson常用方法

//对于一个请求体,json格式,封装的过程
JSONObject jsonObject = new JSONObject();
jsonObject.put("key1", value1);
jsonObject.put("key2", value2);
jsonObject.put("key3", value3);
//若有子对象,多封装一层
JSONObject subJson= new JSONObject();
subJson.put("key41", value41);
subJson.put("key42", value42);

jsonObject.put("key4", subJson);


//得到json串
String requestBody = jsonObject.toJSONString();
---------------------------
{
	"key1": value1,
	"key2": value2,
	"key3": value3,
	"key4": {
		"key41": value41,
		"key42": value42
	} 
}
------------String转为JSONObject---------------
JSONObject jsonObject = JSON.parseObject(JSON);
String v1 = jsonObject.getString("key1");//getxxx() 不同的xxx方法能得到xxx类型


----------StringJSON串转对象,相同对应‘key-属性‘会赋值---------
Object object = JSONObject.parseObject(Json,  Object.class);
  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

BiuPsYao

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值