欢迎来到站圈网!

java numpy线性方程组KEY_Oc36cfac82e3python线性插值pygameselenium

投稿中心

Java

当前位置: 主页 > 编程语言 > Java

Java遍历Json中的key和value问题

时间:2023-11-20|作者:|点击:

Java遍历Json中的key和value

最近对接了不少别人家的系统,他们的签名验签大多采用业务数据值拼接之后进行加密动作

这个时候遍历出对象中的key和value就有为方便,

因此有以下现成代码:

 
    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();
    }

Java遍历Json中的key和value问题

遍历获取JSONObject的所有Key

JSON解析使用JSONObject.keys()可以获取所有的key值,但是这种方法只能获取一层:

比如{"b":"2","c":{"A":"1","B":"2"},"a":"1"},只能够获取b,c,a

如果想要获取被嵌套的{"A":"1","B":"2"}中A,B就不可以了

自己实现了一下获取嵌套类型的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字符不是数组或根本就不是JSON
			try {
				JSONObject object = new JSONObject(sJSON);
				return 1;
			} catch (JSONException e2) {// 抛错 说明JSON字符根本就不是JSON
				System.out.println("非法的JSON字符串");
				return 0;
			}
		}
 
	}

总结

以上为个人经验,希望能给大家一个参考,也希望大家多多支持编程客栈(www.zhano.cn)。

版权声明:本文内容由小编收集网络,均来自网络用户自发贡献,版权归原作者所有,Web前端、电脑编程学习类网站不拥有其著作权,亦不承担相应法律责任。具体规则请查看《Web前端、电脑编程学习类网站用户服务协议》和《Web前端、电脑编程学习类网站知识产权保护指引》。如果您发现本站中有涉嫌抄袭的内容,填写联系本站管理员,一经查实,管理员将立刻删除涉嫌侵权内容。

上一篇:快速查询nodejs版本信息的六种方法

下一篇:解决Error:(5,55)java:程序包org.spri

本文标题:Java遍历Json中的key和value问题

本文地址:https://www.zhano.cn/Java/70947.html

AD300

广告投放 | 联系我们 | 版权申明 | SiteMap

重要申明:本站所有的文章、图片、评论等,均由网友发表或上传并维护或收集自网络,属个人行为,与本站立场无关。

如果侵犯了您的权利,请与我们联系,我们将在24小时内进行处理、任何非本站因素导致的法律后果,本站均不负任何责任。

联系QQ:15234109 | 邮箱:15234109#qq.com(#换成@)

Copyright © 2019-2023 豫ICP备19001789号