Java遍歷Json中的key和value問題
Java遍歷Json中的key和value
最近對接了不少別人家的系統(tǒng),他們的簽名驗簽大多采用業(yè)務數(shù)據(jù)值拼接之后進行加密動作
這個時候遍歷出對象中的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就不可以了
自己實現(xiàn)了一下獲取嵌套類型的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是判斷一個字符串是array類型還是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) {// 拋錯 說明JSON字符不是數(shù)組或根本就不是JSON try { JSONObject object = new JSONObject(sJSON); return 1; } catch (JSONException e2) {// 拋錯 說明JSON字符根本就不是JSON System.out.println("非法的JSON字符串"); return 0; } } }
總結
以上為個人經(jīng)驗,希望能給大家一個參考,也希望大家多多支持腳本之家。
相關文章
java8 利用reduce實現(xiàn)將列表中的多個元素的屬性求和并返回操作
這篇文章主要介紹了java8 利用reduce實現(xiàn)將列表中的多個元素的屬性求和并返回操作,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2020-08-08SpringBoot快速接入OpenAI大模型的方法(JDK8)
本文介紹了如何使用AI4J快速接入OpenAI大模型,并展示了如何實現(xiàn)流式與非流式的輸出,以及對函數(shù)調(diào)用的使用,AI4J支持JDK8,適用于多種應用場景,包括Spring Boot項目,感興趣的朋友一起看看吧2025-02-02Springboot中的@ComponentScan注解使用解析
這篇文章主要介紹了Springboot中的@ComponentScan注解使用解析,@ComponentScan用于類或接口上主要是指定掃描路徑,spring會把指定路徑下帶有指定注解的類注冊到IOC容器中,需要的朋友可以參考下2024-01-01使用Spring?Boot如何限制在一分鐘內(nèi)某個IP只能訪問10次
有些時候,為了防止我們上線的網(wǎng)站被攻擊,或者被刷取流量,我們會對某一個ip進行限制處理,這篇文章,我們將通過Spring?Boot編寫一個小案例,來實現(xiàn)在一分鐘內(nèi)同一個IP只能訪問10次,感興趣的朋友一起看看吧2023-10-10