關於JSON的解析問題以及常規用法

JSON解析

String jsonString ="{\"name\":\"zhangsan\",\"password\":\"zhangsan123\",\"email\":\"[email protected]\"}";  
         JSONObject json = JSONObject.fromObject(jsonString);  
         User user = new User();  
         user.setName(json.getString("name"));  
         user.setPassword(json.getString("password"));  
         user.setEmail(json.getString("email"));  
         System.out.println(user.toString());  


String json = <span style="color: rgb(51, 51, 51); font-family: Arial; font-size: 14px; line-height: 26px;">"[{\"name\":\"zhangsan\",\"password\":\"zhangsan123\",\"email\":\"[email protected]\"},</span><span style="color: rgb(51, 51, 51); font-family: Arial; font-size: 14px; line-height: 26px;">{\"name\":\"lisi\",\"password\":\"lisi123\",\"email\":\"[email protected]\"}</span><span style="color: rgb(51, 51, 51); font-family: Arial; font-size: 14px; line-height: 26px;">]"</span>;  
         JSONArray jsonArray = JSONArray.fromObject(json);  
         ArrayList<User> users = new ArrayList<User>();  
         for (int i = 0; i < jsonArray.size(); i++) {  
             User userM = new User();  
             user.setName(jsonArray.getJSONObject(i).getString("name"));  
             user.setpassword(jsonArray.getJSONObject(i).getString("password"));  
             user.setEmail(jsonArray.getJSONObject(i).getString("email"));  
             users.add(user);  
        }  
         for (User user : users) {  
            System.out.println(user.toString());
        } 





String goodsSet="[{\"specAttributes\":{\"3216\":\"深灰色\",\"450\":\"S\"},\"amountOnSale\":\"2000\",\"price\":\"103.0\",\"retailPrice\":\"103.0\"},{\"specAttributes\":{\"3216\":\"深灰色\",\"450\":\"M\"},\"amountOnSale\":\"1995\",\"price\":\"103.0\",\"retailPrice\":\"103.0\"},{\"specAttributes\":{\"3216\":\"深灰色\",\"450\":\"L\"},\"amountOnSale\":\"1998\",\"price\":\"103.0\",\"retailPrice\":\"103.0\"},{\"specAttributes\":{\"3216\":\"深灰色\",\"450\":\"XL\"},\"amountOnSale\":\"1996\",\"price\":\"103.0\",\"retailPrice\":\"103.0\"}]";

JSONArray array = new JSONArray(goodsSet);
			List<GoodsSetVo> goodsSetVolist=new ArrayList<GoodsSetVo>();
		//	Integer amountOnSale=null;
			for (int i = 0; i < array.length(); i++) {
			   JSONObject iObj = array.getJSONObject(i);
	//	       System.out.println(iObj.get("specAttributes"));//規格屬性
	//	       System.out.println(iObj.get("amountOnSale"));//銷量
	//	       System.out.println(iObj.get("price"));       //價格
	//	       System.out.println(iObj.get("retailPrice"));  //零售價
		       
	           JSONObject jsonObject = iObj.getJSONObject("specAttributes");
	//           System.out.println(jsonObject.get("450"));   //尺寸
	//	       System.out.println(jsonObject.get("3216"));  //顏色
		       //組成對象
		       GoodsSetVo goodSet=new GoodsSetVo();
		       goodSet.setCreateTime(new Date());
		       goodSet.setIsDel(0);
		       goodSet.setIsShow(1);
		       goodSet.setSort(1);  
		       if(jsonObject.has("3216")) {
		    	   goodSet.setSizeColorName(jsonObject.get("3216").toString());
		    	   if(jsonObject.has("450")) {
		    		   goodSet.setSizeColorName(jsonObject.get("3216").toString()+"/"+jsonObject.get("450").toString());  
		    	   }
		       }else {
		    	   if(jsonObject.has("450")) {
		    		   goodSet.setSizeColorName(jsonObject.get("450").toString());
		    	   }
		       }
		       goodsSetVolist.add(goodSet);
			}
			return goodsSetVolist;

 

 

java中,判斷JSONObject是否存在某個Key

使用has()方法,

if(jo.has("msg")){
}

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章