遍历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);