Java遍歷Json中的key和value問(wèn)題
Java遍歷Json中的key和value
最近對(duì)接了不少別人家的系統(tǒng),他們的簽名驗(yàn)簽大多采用業(yè)務(wù)數(shù)據(jù)值拼接之后進(jìn)行加密動(dòng)作
這個(gè)時(shí)候遍歷出對(duì)象中的key和value就有為方便,
因此有以下現(xiàn)成代碼:
public String appendSignData(JSONObject obj){
StringBuffer sb = new StringBuffer();
//fastjson解析方法
for (Map.Entry<String, Object> entry : obj.entrySet()) {
System.out.println("key值="+entry.getKey());
sb.append(entry.getValue());
}
return sb.toString();
}
遍歷獲取JSONObject的所有Key
JSON解析使用JSONObject.keys()可以獲取所有的key值,但是這種方法只能獲取一層:
比如{"b":"2","c":{"A":"1","B":"2"},"a":"1"},只能夠獲取b,c,a
如果想要獲取被嵌套的{"A":"1","B":"2"}中A,B就不可以了
自己實(shí)現(xiàn)了一下獲取嵌套類(lèi)型的JSONObject的所有key值
import java.util.Iterator;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
public class testGetKeys {
public static String getKeys(JSONObject test) throws JSONException{
String result = null;
testJsonCompare t = new testJsonCompare();
Iterator keys = test.keys();
while(keys.hasNext()){
try{
String key = keys.next().toString();
String value = test.optString(key);
int i = t.testIsArrayORObject(value);
if(result == null || result.equals("")){
if(i == 0){
result = key + ",";
System.out.println("i=0 | key="+key+"| result="+result);
}else if( i == 1){
result = key + ",";
System.out.println("i=1 | key="+key+"| result="+result);
result = getKeys(new JSONObject(value))+",";
}else if( i == 2){
result = key + ",";
System.out.println("i=2 | key="+key+"| result="+result);
JSONArray arrays = new JSONArray(value);
for(int k =0;k<arrays.length();k++){
JSONObject array = new JSONObject(arrays.get(k));
result = getKeys(array) + ",";
}
}
}else{
if(i == 0){
result = result + key + ",";
System.out.println("i=0 | key="+key+"| result="+result);
}else if( i == 1){
result = result + key + ",";
System.out.println("i=1 | key="+key+"| result="+result);
result = result + getKeys(new JSONObject(value));
}else if( i == 2){
result = result + key + ",";
System.out.println("i=2 | key="+key+"| result="+result);
JSONArray arrays = new JSONArray(value);
for(int k =0;k<arrays.length();k++){
JSONObject array = new JSONObject(arrays.get(k));
result = result + getKeys(array) + ",";
}
}
}
}catch(JSONException e){
e.printStackTrace();
}
}
return result;
}
public static void main(String args[]) throws org.json.JSONException{
JSONObject test = new JSONObject();
JSONObject test1 = new JSONObject();
try{
test1.put("A", "1");
test1.put("B", "2");
test.put("a", "1");
test.put("c", test1);
test.put("b", "2");
System.out.println(test.toString());
}catch(JSONException e){
e.printStackTrace();
}
String s = getKeys(test);
System.out.println(s);
}
}testIsArrayORObject是判斷一個(gè)字符串是array類(lèi)型還是object
public int testIsArrayORObject(String sJSON){
/*
* return 0:既不是array也不是object
* return 1:是object
* return 2 :是Array
*/
try {
JSONArray array = new JSONArray(sJSON);
return 2;
} catch (JSONException e) {// 拋錯(cuò) 說(shuō)明JSON字符不是數(shù)組或根本就不是JSON
try {
JSONObject object = new JSONObject(sJSON);
return 1;
} catch (JSONException e2) {// 拋錯(cuò) 說(shuō)明JSON字符根本就不是JSON
System.out.println("非法的JSON字符串");
return 0;
}
}
}總結(jié)
以上為個(gè)人經(jīng)驗(yàn),希望能給大家一個(gè)參考,也希望大家多多支持腳本之家。
相關(guān)文章
SpringBoot基于Redis實(shí)現(xiàn)token的在線續(xù)期的實(shí)踐
本文主要介紹了使用Redis實(shí)現(xiàn)JWT令牌在線續(xù)期的方案,通過(guò)在線續(xù)期token,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2024-12-12
關(guān)于SpringBoot配置文件application.properties的路徑問(wèn)題
這篇文章主要介紹了關(guān)于SpringBoot配置文件application.properties的路徑問(wèn)題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2022-08-08
java中instanceof和getClass()的區(qū)別分析
本篇文章介紹了,在java中instanceof和getClass()的區(qū)別分析。需要的朋友參考下2013-04-04
數(shù)組實(shí)現(xiàn)Java 自定義Queue隊(duì)列及應(yīng)用操作
這篇文章主要介紹了數(shù)組實(shí)現(xiàn)Java 自定義Queue隊(duì)列及應(yīng)用操作,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2021-06-06
spring中@ControllerAdvice 注解的使用
@ControllerAdvice注解是Spring3.2中新增的注解,主要用于Controller的全局配置,本文就來(lái)介紹一下spring中@ControllerAdvice 注解的使用,感興趣的可以了解一下2024-09-09
Java高并發(fā)編程之CAS實(shí)現(xiàn)無(wú)鎖隊(duì)列代碼實(shí)例
這篇文章主要介紹了Java高并發(fā)編程之CAS實(shí)現(xiàn)無(wú)鎖隊(duì)列代碼實(shí)例,在多線程操作中,我們通常會(huì)添加鎖來(lái)保證線程的安全,那么這樣勢(shì)必會(huì)影響程序的性能,那么為了解決這一問(wèn)題,于是就有了在無(wú)鎖操作的情況下依然能夠保證線程的安全,需要的朋友可以參考下2023-12-12
Springboot項(xiàng)目啟動(dòng)找不到啟動(dòng)類(lèi)的解決
這篇文章主要介紹了Springboot項(xiàng)目啟動(dòng)找不到啟動(dòng)類(lèi)的解決方案,具有很好的參考價(jià)值,希望對(duì)大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2024-08-08

