Java数组转Json数组

package com.cnic.test.coding;

import com.alibaba.fastjson.JSONArray;

public class ArrToJson {
    public static void main(String[] args) {
        int[] my = new int[5];
        my[0] = 0;
        my[1] = 1;
        my[2] = 2;
        my[3] = 3;
        my[4] = 4;
        // 数组转为 JSONArray
        JSONArray jsonArray = (JSONArray) JSONArray.toJSON(my);
        System.out.println(jsonArray instanceof JSONArray);
        System.out.println(jsonArray.getClass().isArray());
        System.out.println(jsonArray.toString());
        try {
            JSONArray jsonObj = (JSONArray) JSONArray.toJSON(my);// 数组转为JsonArray
            System.out.println(jsonObj instanceof JSONArray);// 是否是Json数组?
            System.out.println(jsonObj.getClass().isArray());// 是否为数组?
            String jsonStr = jsonObj.toString();// JsonArray转为String
            System.out.println(jsonStr instanceof String);// 是否为String?
            System.out.println(jsonStr);
        } catch (Exception e) {
            System.out.println("数组转json失败");
        }
        
    }

}

你可能感兴趣的:(Java,Flink,java,json,开发语言)