java 修改JsonObject对象所有的Value类型为String

发布时间:2024年01月11日

将JSONObject 或者JSONArray 中所有Value 为数值类型 转为String.

转换前:

[{
	"zjlx": 201,
	"xm": "刘**",
	"cbdjxxlist": [{
		"zspmdm": 102031201,
		"rybm": "43000010300000411195",
		"jfrlx": 1,
		"zszmdm": "000000000",
		"zsxmdm": 10203,
		"sbjbjgdm": 4303994300003100000,
		"ksjfny": 200912,
		"dsdmname": "湘潭市本级",
		"zgswjgdm": 14303040000,
		"zzjfny": 209912
	}],
	"djxh": "20124300910055880605",
	"zjhm": 43030411040
}, {
	"zjlx": 201,
	"xm": "刘**",
	"cbdjxxlist": [{
		"zspmdm": 102031701,
		"rybm": "43000010300000411195",
		"jfrlx": 1,
		"zszmdm": "000000000",
		"zsxmdm": 10203,
		"sbjbjgdm": 4303994300003100000,
		"ksjfny": 200912,
		"dsdmname": "湘潭市本级",
		"zgswjgdm": 14303040000,
		"zzjfny": 209912
	}],
	"djxh": "20124300910055880605",
	"zjhm": 43030411040
}, {
	"zjlx": 201,
	"xm": "刘**",
	"cbdjxxlist": [{
		"zspmdm": 102032602,
		"rybm": "43000010300000411195",
		"jfrlx": 1,
		"zszmdm": "000000000",
		"zsxmdm": 10203,
		"sbjbjgdm": 4303994300003100000,
		"ksjfny": 202109,
		"dsdmname": "湘潭市本级",
		"zgswjgdm": 14303040000,
		"zzjfny": 209912
	}],
	"djxh": "20124300910055880605",
	"zjhm": 43030411040
}]

转换后:

[{
	"zjlx": "201",
	"xm": "刘**",
	"cbdjxxlist": [{
		"ksjfny": "200912",
		"zspmdm": "102031201",
		"rybm": "43000010300000411195",
		"jfrlx": "1",
		"zszmdm": "000000000",
		"dsdmname": "湘潭市本级",
		"zgswjgdm": "14303040000",
		"zsxmdm": "10203",
		"sbjbjgdm": "4303994300003100000",
		"zzjfny": "209912"
	}],
	"djxh": "20124300910055880605",
	"zjhm": "4303041****1040"
}, {
	"zjlx": "201",
	"xm": "刘**",
	"cbdjxxlist": [{
		"ksjfny": "200912",
		"zspmdm": "102031701",
		"rybm": "43000010300000411195",
		"jfrlx": "1",
		"zszmdm": "000000000",
		"dsdmname": "湘潭市本级",
		"zgswjgdm": "14303040000",
		"zsxmdm": "10203",
		"sbjbjgdm": "4303994300003100000",
		"zzjfny": "209912"
	}],
	"djxh": "20124300910055880605",
	"zjhm": "4303041****1040"
}, {
	"zjlx": "201",
	"xm": "刘**",
	"cbdjxxlist": [{
		"ksjfny": "202109",
		"zspmdm": "102032602",
		"rybm": "43000010300000411195",
		"jfrlx": "1",
		"zszmdm": "000000000",
		"dsdmname": "湘潭市本级",
		"zgswjgdm": "14303040000",
		"zsxmdm": "10203",
		"sbjbjgdm": "4303994300003100000",
		"zzjfny": "209912"
	}],
	"djxh": "20124300910055880605",
	"zjhm": "4303041****1040"
}]

代码

public void modifyJsonNumberValue(Object object) {
        if (object instanceof JSONObject) {
            JSONObject jsonObject = (JSONObject) object;
            Iterator iterator = jsonObject.entrySet().iterator();
            while (iterator.hasNext()) {
                Map.Entry entry = (Map.Entry) iterator.next();
                Object o = entry.getValue();
                if (o instanceof Long || o instanceof Integer || o instanceof Double) {
                    if (entry.getKey() != null) {
                        jsonObject.put(entry.getKey().toString(), o + "");
                    }
                } else if (o instanceof JSONArray) {
                    modifyJsonNumberValue(o);
                } else if (o instanceof JSONObject) {
                    modifyJsonNumberValue(o);
                }
            }
        }
        if (object instanceof JSONArray) {
            JSONArray jsonArray = (JSONArray) object;
            for (int i = 0; i < jsonArray.size(); i++) {
                modifyJsonNumberValue(jsonArray.get(i));
            }
        }
    }

文章来源:https://blog.csdn.net/qq_38103371/article/details/135519100
本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。