当前位置: 代码迷 >> 综合 >> 6.JSONArray、JSONObject的使用(详细明了例子)
  详细解决方案

6.JSONArray、JSONObject的使用(详细明了例子)

热度:67   发布时间:2023-12-02 23:30:30.0

代码如下:首先创建几个类Student、Dog、Cat

    class Student {
    int age;String name;Student(int age, String name) {
    this.age = age;this.name = name;}public String getName() {
    return name;}public int getAge() {
    return age;}public void setAge(int a) {
    this.age = a;}}@Dataclass Dog{
    String name;String hobby;public  Dog(String name,String hobby){
    this.name=name;this.hobby=hobby;}}@Dataclass Cat{
    String name;String food;public Cat(String name,String food){
    this.name=name;this.food=food;}}

一、JSONObject与字符串之间的转换

  public void test5(){
    String str = "{\"name\":\"里斯\",\"age\":12}";//(1)字符串转为JSONObjectJSONObject jsonObject2 = JSONObject.parseObject(str);String name2 = jsonObject2.getString("name");Integer age2 = jsonObject2.getInteger("age");System.out.println(name2);//里斯System.out.println(age2);//12//(2)JSONObject转为字符串String s = jsonObject2.toJSONString();System.out.println(s);//{"name":"里斯","age":12}
}

从上述代码中可以看出,JSONObject的数据用{}来表示,如{“name”:“里斯”,“age”:12}

二、JSONObject与实体对象之间的转换

public void testMethod() {
    String str = "{\"fee\":1105.13,\"meteringElec\":8.8,\"price\":283.95,\"settleElec\":3.892}";JSONObject jsonObject = JSONObject.parseObject(str);System.out.println(jsonObject.toString());//{"meteringElec":8.8,"settleElec":3.892,"price":283.95,"fee":1105.13}System.out.println(jsonObject.toJSONString());//{"meteringElec":8.8,"settleElec":3.892,"price":283.95,"fee":1105.13}DaySettleValueVo vo = JSONObject.parseObject(str, DaySettleValueVo.class);System.out.println(vo.getFee());System.out.println(vo.getPrice());System.out.println(vo.getSettleElec());}

三、JSONArray与字符串之间的转换

public void test6() {
    Student model1 = new Student(123, "ls");Student model2 = new Student(18, "lgr");Student model3 = new Student(100, "ls");JSONArray jsonArray = new JSONArray();jsonArray.add(model1);jsonArray.add(model2);jsonArray.add(model3);//(1)jsonArray转为字符串System.out.println(jsonArray);//[{"age":123,"name":"ls"},{"age":18,"name":"lgr"},{"age":100,"name":"ls"}]System.out.println(jsonArray.toJSONString());//[{"age":123,"name":"ls"},{"age":18,"name":"lgr"},{"age":100,"name":"ls"}]//(2)字符串转为jsonArrayJSONArray jsonArray2 = JSONArray.parseArray(jsonArray.toJSONString());}

从代码中输出的结果可以看出,JSNOArray是由JSONObject构成的数组,用[{},{},{},……{}]来表示。

四、JSONArray与集合之间的转换

     JSONArray jsonArray = new JSONArray();jsonArray.addAll(Arrays.asList(1.0,2.0,33.0));//jsonArray存入类型为BigDecimal的元素List<BigDecimal> bigDecimals = JSONArray.parseArray(jsonArray.toJSONString(), BigDecimal.class);System.out.println(bigDecimals);//[1.0, 2.0, 33.0]JSONArray jsonArray1 = new JSONArray();jsonArray1.add(Arrays.asList(2.0,3.0,40.0));jsonArray1.add(Arrays.asList(11.0,12.0,420.0));//jsonArray1存入类型为List<BigDecimal>的元素List<List> lists = JSONArray.parseArray(jsonArray1.toJSONString(), List.class);System.out.println(lists);//[[2.0, 3.0, 40.0], [11.0, 12.0, 420.0]]

五、JSONObject与JSONArray之间的转换

 public void test6() {
    Student student= new Student(12,"里斯");Dog dog1 = new Dog("A","running");Dog dog2 = new Dog("B","sleeping");Cat cat1 = new Cat("CC","fish");Cat cat2 = new Cat("DD" ,"mouse");Cat cat3 = new Cat("EE","rice");//(1)向JSONObject put类型为JSONArray的元素JSONArray catArray = new JSONArray();catArray.add(cat1);catArray.add(cat2);catArray.add(cat3);JSONArray dogArray = new JSONArray();dogArray.add(dog1);dogArray.add(dog2);JSONObject jSONObject1 = new JSONObject();
// jSONObject1.put("cat",new Cat[]{cat1,cat2,cat3});jSONObject1.put("cat",catArray);
// jSONObject1.put("dog",new Dog[]{dog1,dog2});jSONObject1.put("dog",dogArray);JSONObject jSONObject2 = new JSONObject();jSONObject2.put("student",student);jSONObject2.put("animal",jSONObject1);System.out.println(jSONObject2.toJSONString());//{"student":{"age":12,"name":"里斯"},"animal":{"cat":[{"food":"fish","name":"CC"},{"food":"mouse","name":"DD"},{"food":"rice","name":"EE"}],"dog":[{"hobby":"running","name":"A"},{"hobby":"sleeping","name":"B"}]}}//(2)从JSONObject get JSONArray类型的元素JSONObject animalObject = jSONObject2.getJSONObject("animal");JSONArray catJSONArray = animalObject.getJSONArray("cat");JSONArray dogJSONArray = animalObject.getJSONArray("dog");System.out.println(catJSONArray);//[{"food":"fish","name":"CC"},{"food":"mouse","name":"DD"},{"food":"rice","name":"EE"}]System.out.println(dogJSONArray);//[{"hobby":"running","name":"A"},{"hobby":"sleeping","name":"B"}]String name = catJSONArray.getJSONObject(1).getString("name");System.out.println(name);//DD//(3)向JSONArray add 类型为JSONObject的元素JSONObject jsonObject1 = new JSONObject();jsonObject1.put("key1", "value1");JSONObject jsonObject2 = new JSONObject();jsonObject2.put("key2", "value2");JSONArray jsonArray = new JSONArray();jsonArray.add(jsonObject1);jsonArray.add(jsonObject2);System.out.println(jsonArray);//[{"key1":"value1"},{"key2":"value2"}]//(4)从JSONArray get JSONObject类型的元素Object o = jsonArray.get(0);System.out.println(o);//{"key1":"value1"}}

六、总结

JSONObject的数据格式与map相似,都是key-value的形式。
JSONArray是由多个Object元素组成的数组(当然JSONObject也可以作为JSONArray的元素)。
而同时JSONArray数组也可以作为JSONObject的value值。
从上述几段代码可以直观的理解JSONArray和JSONObject的特性。

  相关解决方案