java 解析json 常用方法 --前端与后端的交互(后端)

本篇为后端解析json篇,需要了解前端js解析json的可以返回翻阅。前端js解析json

JSON是使用比较广泛的一种数据传输格式,是我们程序猿开发的必备技能之一,当然XML也是比较熟悉的数据交换方式,但XML使用起来比较复杂,不方便,而且标记比数据多,感觉比较浪费流量,JSON就相对比较轻快、方便,没有任何的标记,特别是在JS中使用JSON更能显示出其优点。用的时候比较方便,解析也不复杂,现在详说后端解析json的方法,除了使用JSON一些官方基本jar包解析json之外,当然还可以使用GSON类库进行解析,GSON比JSON强大;现在先从简单说起。
1.后端解析json的前提条件,需要的JSON包(版本自行,以下仅供参考):
(1) json-lib-2.3-jdk15.jar
(2)commons-beanutils-1.7.0.jar
(3)commons-lang-2.3.jar
(4)commons-logging-1.0.4.jar
(5)commons-collections-3.1.jar
(6)ezmorph-1.0.3.jar

这些包可以从一下地方下载:
http://commons.apache.org/index.html
http://json-lib.sourceforge.net/
http://ezmorph.sourceforge.net/
http://www.docjar.com/

2.后端解析json的常用方法:
直接上代码:这些方法都是基本需要掌握的,更深层次的后面再说(PS:JSONObjec能做的Map基本都能实现;不同的是JSONObject的value值不能为null值,Map可以为null值)
2.1JSONObject解析:

  public static void main(String[] args) {
    	/**
    	 * 1.后端基本json的解析
    	 * */
    		//模拟需要解析的json数据
    		String jsonStr=getJsonStr();
    		//将json字符串转json格式
    /*		
     * 1.1如果json字符串格式是数组,如[{"key1":"value1",...},{...}...],解析如下:
     * */
    		JSONArray jsonArr= JSONArray.fromObject(jsonStr);
    		//1.2如果json字符串格式是对象,如{"key1":"value1",...},解析如下:
    		 //JSONObject jsonObj=JSONObject.fromObject(jsonStr);
    		/*
    		 * 1.3解析方法:
    		 * */
    		for(int i=0;i<jsonArr.size();i++){			
    			JSONObject jsonObj=jsonArr.getJSONObject(i);//转json对象,使用jsonArr.optJSONObject()方法不会抛异常
    			//1.3.1 二次取值,先拿到key,再拿value;(如果使用Map也可以这样解析,jsonObject能解析的Map基本也能解析)
    			for(Object key:jsonObj.keySet()){
    				System.out.println("key:"+key+",value:"+jsonObj.get(key));
    			}
    			//1.3.2 使用迭代器取值,多用于对key值进行判断
    			Iterator<Map.Entry<String, String>> it= jsonObj.entrySet().iterator();
    			while(it.hasNext()){
    				Entry<String, String> entry=it.next();
    				System.out.println("key:"+entry.getKey()+",value:"+entry.getValue());
    			}
    			//1.3.3 直接取value值
    			for(Object val:jsonObj.values()){
    				System.out.println("values:"+val);
    			}
    		}	
    	}
    	/*  
    	 * 构造json数据格式
    	*/  
    public static String getJsonStr(){
    	
    
    			//创建json对象
    			JSONObject jsonObj=new JSONObject();
    			//创建json数组
    		    JSONArray jsonArr=new JSONArray();
    		  //jsonObj对象添加数据
    		    jsonObj.put("key1", "value1");
    		    jsonObj.put("key2", "value2");
    		    jsonObj.put("key3", "value3");
    		    //jsonArr数组存对象数组
    		    jsonArr.add(jsonObj);
                /*或者使用Map的方法创建json对象数据
    			Map<String, String> map=new HashedMap();
    			map.put("key1", "value1");
    			jsonArr.add(map);
    			*/ 
    		    //将json数据转换成string
    		    return jsonArr.toString();
    		}

2.2 顺便说一下Map解析及一些基本方法应用:

/*
 * map集合接口的实现类有三个:HashMap、TreeMap、LinkedHashMap
 * 利用三个实现类可以往map集合里面添加元素,以key和value方式保存
 * 其中HashMap实现类添加元素是无序的,LinkedHashMap、Treemap是按添加顺序保存元素的
 * */

public class Map_Test {
    public static void main(String[]args){
    	Map<String, Integer> hashmap=new HashMap<String, Integer>();
    	Map<String, Integer> treemap=new TreeMap<String, Integer>();
    	Map<String,Integer>  linkedhashmap=new LinkedHashMap<String, Integer>();
    	System.out.println("hashmap:");
    	testmap(hashmap);
    	System.out.println("treemap:");
    	testmap(treemap);
    	System.out.println("linkedhashmap:");
    	testmap(linkedhashmap);
    	System.out.println("------------------------------------");

    	Map<String , String> map=new HashMap<String,String>();
    	map.put("key1", "value1");
    	map.put("key2", "value2");
    	map.put("key3", "value3");
    	map.put("key4", "value4");
    	map.put("key5", "value5");
    	//1.二次取值,先拿到key,再拿value
    	System.out.println("第一种方法遍历:通过map.keyset方法获取key,然后再取value");
    	for(String key:map.keySet()){
    		System.out.println("key:"+key+"value:"+map.get(key));
    	}
    	//2.多用于判断值是否为空
    	System.out.println("第二种方法遍历:通过map.entryset使用iterator遍历");
    	Iterator<Map.Entry<String, String>> it=map.entrySet().iterator();
    	while(it.hasNext()){
    		Entry<String, String> entry=it.next();
    		System.out.println("key:"+entry.getKey()+""+"value:"+entry.getValue());
    	}
    	//3.容量大的时候推荐使用,直接使用
    	System.out.println("第三种方法遍历,通过使用map.entryset方法遍历key和value");
    	for(Entry<String,String> entry:map.entrySet()){
    		System.out.println("key:"+entry.getKey()+""+"value"+entry.getValue());
    	}
    	//4.仅仅获取values
    	System.out.println("第四种方法遍历,使用map.values方法获取所有的value,但不能获取到key");
    	for(String val:map.values()){
    		System.out.println("values:"+val);
    	}
    }
    //测试map集合的三种实现类的顺序
    public static void testmap(Map<String, Integer> map){
    	map.put("2asd", 5);
    	map.put("3asd", 4);
    	map.put("4asd", 3);
    	map.put("5asd", 2);
    	map.put("6asd", 1);
    	
    	for(Map.Entry<String, Integer> entry :map.entrySet()){
    		System.out.println("key:"+entry.getKey()+" "+"value:"+entry.getValue());
    	}
    }
}   

猜你喜欢

转载自blog.csdn.net/use_admin/article/details/82810113