JSONObject常用转换

JSONObject常用转换,第1张

JSONObject常用转换
package com.neusoft.ehrss.base.si.person.appraisePlat.utils;

import com.alibaba.fastjson.JSONObject;


public class JsonUtil {


    
    public static String jsonObjectToString(JSonObject jsonObject) {
        return jsonObject.toJSonString();
    }

    
    public static String objectToJson(Object obj) {
        return JSONObject.toJSonString(obj);
    }


    
    public static Object jsonToObject(String jsonStr) {
        return JSONObject.parseObject(jsonStr, Object.class);

    }

    
    public static JSonObject jsonToJSONObject(String jsonObj) {
        return JSONObject.parseObject(jsonObj);
    }


    
    public static Object jsonObjectToObject(JSonObject jso) {
        return JSONObject.toJavaObject(jso, Object.class);
    }


    
    public static JSonObject objectToJSonObject(Object obj) {
        return JSONObject.parseObject(JSONObject.toJSonString(obj));
    }

    public static void main(String[] args) {
        Student student = new Student();
        student.setIdNumber("220302200011170321");
        student.setName("测试");
        String str = objectToJson(student);
        System.out.println("java对象转换为JSON字符串" + str);

        String jsonStr = "{"idNumber":"220302200011170321","name":"测试"}";
        Object obj = jsonToObject(jsonStr);
        System.out.println("json字符串转换为JSONObject对象" + obj);

        String jsonObj = "{"idNumber":"220302200011170321","name":"测试"}";
        JSonObject jso = jsonToJSONObject(jsonObj);
        System.out.println("JSON字符串转换为java对象" + jso);

        JSonObject jsb = new JSonObject();
        jsb.put("age", "18");
        Object obj1 = jsonObjectToObject(jsb);
        System.out.println("将JSONObject对象转换为java对象" + obj1);

        JSonObject jsonObject = objectToJSonObject(student);
        System.out.println("将java对象转换为JSONObject对象" + jsonObject);


        String str1 = jsonObjectToString(jsb);
        System.out.println("将JSONObject对象转成String字符串" + str1);
    }
}

欢迎分享,转载请注明来源:内存溢出

原文地址:https://54852.com/zaji/5609448.html

(0)
打赏 微信扫一扫微信扫一扫 支付宝扫一扫支付宝扫一扫
上一篇 2022-12-15
下一篇2022-12-15

发表评论

登录后才能评论

评论列表(0条)

    保存