java解析json格式的字符串 maven依赖

1.解析简单的josn格式字符串

String msg = "{'rs':0,'fs':'登录成功!'}";

JSONObject jsonObject = new JSONObject(msg);

System.out.println(jsonObject.getString("fs"))

2.解析json数组格式的字符串

String jsonStr = "[{\"assignNo\":\"201310091540\", \"deliveryStatus\":\"66666\",\"id\":\"1\", \"remark\":\"YY\",\"userName\":\"ZHANGSAN\"}, {\"assignNo\":\"3215242251522215\", \"deliveryStatus\":\"33333\",\"id\":\"3\", \"imgName\":\"1381314903597.png\",\"imgStr\": \"1381314903597.png\",\"remark\":\"\", \"userName\":\"ZHANGSAN\"}]";

JSONArray jsonArray = JSONArray.fromObject(jsonStr);

Object[] objs = jsonArray.toArray();

for (Object object : objs) {

JSONObject jsonObject = JSONObject.fromObject(object);

if(jsonObject.has("imgStr")){

System.out.println(jsonObject.getString("imgStr"));

}

System.out.println(jsonObject.getString("assignNo"));

}


String jsonStr =sendPost(url,para,false);
JSONObject jsonObject = JSONObject.fromObject(jsonStr);
if (jsonObject.getBoolean("success") == true){
    //登录成功
    return true;
}else{
    logger.error("密码输入错误!" );
    return false;
}


下面是通过maven进行添加依赖



    net.sf.json-lib
    json-lib
    2.4
    jdk15

//排除roles属性不做序列化
JsonConfig jsonConfig = new JsonConfig();  
jsonConfig.setExcludes(new String[]{ "roles" });
        
//注意,如果json对象包含了为null的值,转换将会报错
JSONObject.fromObject(json, jsonConfig);


你可能感兴趣的:(java解析json格式的字符串 maven依赖)