json字符串的一些操作(不断添加)

1. 判断当前json字符串是数组还是对象
    public void objectOrArray() {
        String data = "{\"person\":[{\"age\":\"1\"},{\"age\":\"22\"},{\"age\":\"333\"}]}";
        Object parse = JSONObject.parse(data);

        //方法一
        if (parse instanceof JSONArray) {
            System.out.println("data是一个数组");
        }
        if (parse instanceof JSONObject) {
            System.out.println("data是一个对象");
        }

        //方法二
        try {
            Map map = (Map)parse;
        } catch (Exception e) {
            System.out.println("该数据不属于对象");
        }
        try {
            List list = (List)parse;
        } catch (Exception e) {
            System.out.println("该数据不属于数组");
        }
    }
2. java 对象转为json字符串
    public void test() {
        List<Student> students = new ArrayList<>();
        Student student1 = new Student("alex","12");
        Student student2 = new Student("afra","13");
        Student student3 = new Student("mary", "34");

        students.add(student1);
        students.add(student2);
        students.add(student3);

        List<Life> list = new ArrayList<>();
        Life life1 = new Life("ic1", students);
        Life life2 = new Life("ic2", students);
        Life life3 = new Life("ic3", students);
        list.add(life1);
        list.add(life2);
        list.add(life3);

        Bao bao = new Bao(list);
        String s = JSON.toJSONString(bao);
        Object o = JSON.toJSON(bao);
        System.out.println(s);
        System.out.println(o);
    }
    class Bao implements Serializable {
        public List<Life> list;
        public Bao(List<Life> list) {
            this.list = list;
        }
    }
    class Life  implements Serializable {

        public String ic;
        public List<Student> students;

        public Life(String ic, List<Student> students) {
            this.ic = ic;
            this.students = students;

        }
    }
    class Student  implements Serializable {
        public String name;
        public String age;
        public Student(String name, String age) {
            this.name = name;
            this.age = age;
        }
    }

打印结果

{
  "list": [
    {
      "ic": "ic1",
      "students": [
        {
          "age": "12",
          "name": "alex"
        },
        {
          "age": "13",
          "name": "afra"
        },
        {
          "age": "34",
          "name": "mary"
        }
      ]
    },
    {
      "ic": "ic2",
      "students": [
        {
          "$ref": "$.list[0].students[0]"
        },
        {
          "$ref": "$.list[0].students[1]"
        },
        {
          "$ref": "$.list[0].students[2]"
        }
      ]
    },
    {
      "ic": "ic3",
      "students": [
        {
          "$ref": "$.list[0].students[0]"
        },
        {
          "$ref": "$.list[0].students[1]"
        },
        {
          "$ref": "$.list[0].students[2]"
        }
      ]
    }
  ]
}
{
  "list": [
    {
      "students": [
        {
          "name": "alex",
          "age": "12"
        },
        {
          "name": "afra",
          "age": "13"
        },
        {
          "name": "mary",
          "age": "34"
        }
      ],
      "ic": "ic1"
    },
    {
      "students": [
        {
          "name": "alex",
          "age": "12"
        },
        {
          "name": "afra",
          "age": "13"
        },
        {
          "name": "mary",
          "age": "34"
        }
      ],
      "ic": "ic2"
    },
    {
      "students": [
        {
          "name": "alex",
          "age": "12"
        },
        {
          "name": "afra",
          "age": "13"
        },
        {
          "name": "mary",
          "age": "34"
        }
      ],
      "ic": "ic3"
    }
  ]
}

仔细观察也能发现两个方法打印出来的还是有区别的

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值