代码如下:

package com.example;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;


public class JsonToLibTest {


    public static void main(String[] args) {
        String str = "{\"cells\": [{\"shape\": \"rect\",\"id\": \"4df7abde-19c3-41e0-a7b3-80917098acb5\",\"visible\": false,\"zIndex\": 23,\"parent\": \"e3ed22a5-8cf4-45a9-835d-43ce4e47bf55\"},{\"shape\": \"rect\",\"id\": \"96e6ff54-db70-4c55-a09d-66b20b42acc7\",\"visible\": false,\"zIndex\": 24,\"parent\": \"e3ed22a5-8cf4-45a9-835d-43ce4e47bf55\"}]}";
        //字符串转换为JSONObject对象
        JSONObject jsonObject = JSONObject.parseObject(str);
        JSONArray jsonArray = null;
        //获取json对象并转换为数组
        jsonArray = jsonObject.getJSONArray("cells");
        for (int i = 0; i < jsonArray.size(); i++) {
            //打印数组
            System.out.println(jsonArray.getJSONObject(i));
        }
        //获取对象中元素属性
        Object shape = jsonArray.getJSONObject(0).get("shape");
        //打印实体属性
        System.out.println(shape.toString());

    }
}

 

打印结果:

{"parent":"e3ed22a5-8cf4-45a9-835d-43ce4e47bf55","visible":false,"shape":"rect","id":"4df7abde-19c3-41e0-a7b3-80917098acb5","zIndex":23}
{"parent":"e3ed22a5-8cf4-45a9-835d-43ce4e47bf55","visible":false,"shape":"rect","id":"96e6ff54-db70-4c55-a09d-66b20b42acc7","zIndex":24}
rect

 

posted on 2021-10-20 16:59  华安√  阅读(2067)  评论(0)    收藏  举报