后端json的用法

遍历JsonArray

// 一个未转化的字符串
String str = "[{name:'a',value:'aa'},{name:'b',value:'bb'},{name:'c',value:'cc'},{name:'d',value:'dd'}]" ; 
 // 首先把字符串转成 JSONArray  对象
JSONArray json = JSONArray.fromObject(str );
if(json.size()>0){
  for(int i=0;i<json.size();i++){
 // 遍历 jsonarray 数组,把每一个对象转成 json 对象
JSONObject job = json.getJSONObject(i); 
// 得到 每个对象中的属性值
System.out.println(job.get("name")+"=") ;  
  }
}


二:遍历JsonObject

JSONObject jsonObject = new JSONObject(s);
//然后用Iterator迭代器遍历取值,建议用反射机制解析到封装好的对象中
JSONObject jsonObject = new JSONObject(jsonString);
        Iterator iterator = jsonObject.keys();
while(iterator.hasNext()){
            key = (String) iterator.next();
        value = jsonObject.getString(key);
}
 


/**
	 * 获取请求中的JSON格式数据
	 *
	 * @param request
	 * @return
	 */
	public static JSONObject getJsonObject(HttpServletRequest request) {
		String data = (String) request.getAttribute("data");
		try {
		} catch (Exception e) {
			e.printStackTrace();
		}
		System.out.println(data);
		JSONObject json = new JSONObject();
		if (data != null && !data.trim().equals("")) {
			json = JSONObject.parseObject(data);
		}

		return json;
	}


	public static JSONObject getJsons(HttpServletRequest request){
		try {
			StringBuffer sb = new StringBuffer();
			BufferedReader br = new BufferedReader(
					new InputStreamReader((ServletInputStream) request.getInputStream(), "utf-8"));
			String temp;
			while ((temp = br.readLine()) != null) {
				sb.append(temp);
			}
			br.close();
			System.out.println(sb.toString());
			JSONObject parse = (JSONObject) JSON.parse(sb.toString());
			request.setAttribute("data",parse);
			return parse;
		}catch (Exception e){
			e.printStackTrace();
			return null;
		}

	}

	public static JSONArray getJsonArray(HttpServletRequest request){
		try {
			StringBuilder sb = new StringBuilder();
			BufferedReader br = new BufferedReader(
					new InputStreamReader((ServletInputStream) request.getInputStream(), "utf-8"));
			String temp;
			while ((temp = br.readLine()) != null) {
				sb.append(temp);
			}
			br.close();
			System.out.println(sb.toString());
			JSONArray json = new JSONArray();
			if (sb != null && !sb.toString().trim().equals("")) {
				json = JSONArray.parseArray(sb.toString());
			}
			return json;
		}catch (Exception e){
			e.printStackTrace();
			return null;
		}
	}
	/**
	 * 
	 * @Title: getIntValue @Description: 将json值转成int @param @param
	 * jsonValue @param @return 设定文件 @return Integer 返回类型 @throws
	 */
	public static Integer getIntValue(Object jsonValue) {
		Integer result = null;
		if (jsonValue != null && !jsonValue.toString().equals("null") && !"".equals(jsonValue.toString())) {
			result = Integer.parseInt(jsonValue.toString());
		}
		return result;
	}

	/**
	 * 
	 * @Title: getBigDecimal @Description: 将json值转成BigDecimal @param @param
	 * value @param @return 设定文件 @return BigDecimal 返回类型 @throws
	 */
	public static BigDecimal getBigDecimal(Object value) {
		BigDecimal ret = null;
		if (value != null) {
			if (value instanceof BigDecimal) {
				ret = (BigDecimal) value;
			} else if (value instanceof String) {
				ret = new BigDecimal((String) value);
			} else if (value instanceof BigInteger) {
				ret = new BigDecimal((BigInteger) value);
			} else if (value instanceof Number) {
				ret = new BigDecimal(((Number) value).doubleValue());
			} else {
				throw new ClassCastException("Not possible to coerce [" + value + "] from class " + value.getClass()
						+ " into a BigDecimal.");
			}
		}
		return ret;
	}

	/**
	 * 转换map中的null为空字符串
	 * 
	 * @param map
	 * @return
	 */
	public static Map<String, Object> mapIsNotNull(Map<String, Object> map) {
		if (map != null) {
			Iterator<Entry<String, Object>> it = map.entrySet().iterator();
			while (it.hasNext()) {
				Entry<String, Object> entry = it.next();
				if (entry.getValue() == null) {
					map.put(entry.getKey(), "");
				}
			}
		}
		return map;
	}
json转成java对象    
      String userString = "[{'id':1,'name':'lz'},{'id':1,'name':'lz'}]";
        JSONObject userJson = JSONObject.parseObject(userString);
        YgAddress user = JSON.toJavaObject(userJson,YgAddress.class);

 

  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值