springboot json和bean和JSONObject,JSONArray的转换

154 篇文章 15 订阅

一 操作说明

1.1 说明

本案例操作说明,json,bean,相互之间转换成JSONObject,jsonarray。

pom依赖的配置

    <!-- ailibaba json转换 -->
    <dependency>
      <groupId>com.alibaba</groupId>
      <artifactId>fastjson</artifactId>
      <version>1.2.28</version>
    </dependency>

1.2 实施操作

1.2.1 定义初始javabean

1.Apple类

public class Apple {
    private String id;
    private String pName;
    private  String price;

    public String getId() {
        return id;
    }

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

    public String getpName() {
        return pName;
    }

    public void setpName(String pName) {
        this.pName = pName;
    }

    public String getPrice() {
        return price;
    }

    public void setPrice(String price) {
        this.price = price;
    }
}

2.city类

public class City {
    private int id;
    private String cName;

    public int getId() {
        return id;
    }

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

    public String getcName() {
        return cName;
    }

    public void setcName(String cName) {
        this.cName = cName;
    }
}

3.province类

public class Province {
    private int  id;
    private String pName;
    private List<City> cityList;

    public int getId() {
        return id;
    }

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

    public String getpName() {
        return pName;
    }

    public void setpName(String pName) {
        this.pName = pName;
    }

    public List<City> getCityList() {
        return cityList;
    }

    public void setCityList(List<City> cityList) {
        this.cityList = cityList;
    }
}

1.2.2 bean和jsonobject,json串

    public static void  jsonObjectInfo(){
        Apple apple=new Apple();
        apple.setId("24444");
        apple.setpName("pingguo234");
        apple.setPrice("345.69");
        Gson gson =new Gson();
      String gjson=  gson.toJson(apple);
        System.out.println("gson:"+gjson);
        //1. 实体变json串
      String tojson=  JSONObject.toJSON(apple)+"";
      System.out.println("tojson:"+tojson);
        //2. json串变JSONObject
        JSONObject appleJsonObject= JSONObject.parseObject(gjson);
        String id=appleJsonObject.get("id")+"";
        String name=appleJsonObject.getString("pName");
        System.out.println("id:"+id+" name: "+name);
    }

调用结果:

 1.2.3  bean和jsonobjectArray

1.代码

    public static void  jsonArrayInfo(){
       Province provice=new Province();
       provice.setId(11);
       provice.setpName("北京");
       List<City> cityList1=new ArrayList<>();
       City city=new City();
       city.setId(11);
       city.setcName("朝阳");
       cityList1.add(city);
        City city2=new City();
        city2.setId(12);
        city2.setcName("海淀");
        cityList1.add(city2);
        City city3=new City();
        city3.setId(13);
        city3.setcName("西城");
        cityList1.add(city3);
       provice.setCityList(cityList1);
       //1.实体转json
        String str = JSONObject.toJSON(provice).toString();
        System.out.println("str: "+str);
       //2.json 转 json array
        JSONObject appObj= JSONObject.parseObject(str);
        String name=appObj.getString("pName");
        System.out.println("pName:"+name);
        //3.获取JSONObject对象中的JSONArray数组
       JSONArray arrayList= appObj.getJSONArray("cityList");
       for(Object obj:arrayList){
           JSONObject jsonObject=(JSONObject)obj;
           String cname=jsonObject.get("cName")+"";
           System.out.println("cname:"+cname);
       }

    }

2.结果

1.3 list转json,转jsonArray

 public static void toArrayList(){
        List<City> cityList1=new ArrayList<>();
        City city=new City();
        city.setId(11);
        city.setcName("朝阳");
        cityList1.add(city);
        City city2=new City();
        city2.setId(12);
        city2.setcName("海淀");
        cityList1.add(city2);
        City city3=new City();
        city3.setId(13);
        city3.setcName("西城");
        cityList1.add(city3);
        //1.实体转json
        String str = JSONObject.toJSON(cityList1).toString();
        System.out.println("str: "+str);
        JSONArray jsonArray= JSONArray.parseArray(str);
        for(int k=0;k<jsonArray.size();k++){
            JSONObject c=(JSONObject) jsonArray.get(k);
           String nt= c.get("cName")+"";
           System.out.println("name:"+nt);
            String cstr = JSONObject.toJSON(c).toString();
            System.out.println("cstr:"+cstr);
        }
    }

 截图

 

  • 2
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值