java获取json数组格式中的值,json字符串转json对象

1、json字符串转json对象

String result = "{\"code\":\"0000\",\"message\":\"查询成功!\",
\"sentDate\":\"2019-03-15\",\"totalNum\":\"3000\",\"result\":
[\"CERT201708300057_20180109_1515464714812\",
\"CERT201708300057_20190209_1515464714811 \",\"CERT201708300057_20190309_1515464714816 \"]}";

JSONObject jsonObject=JSONObject.fromObject(result);
//如
String totalNum = jsonObject.getString("totalNum")

2、获取json数组格式中的值

获取第一点中result中数组的值

JSONArray jsonArray = null;
try {
    jsonArray = new JSONArray(jsonObject.getString("result"));
    for(int i=0;i<jsonArray.length();i++){      
                    System.out.println(jsonArray.get(i));
    }
    } catch (JSONException e) {
        e.printStackTrace();
    }

输出结果为:

CERT201708300057_20180109_1515464714812
CERT201708300057_20190209_1515464714811 
CERT201708300057_20190309_1515464714816 

引用jar:

net.sf.json.JSONObject
com.fr.base.core.json.JSONArray

eg:获取fileId

public static void main(String[] args) {
        String result = "{\"attachs\":[{\"fileId\":10000088060170,\"fileName\":\"wifi密码.png\",\"uploaderId\":\"WU_FILE_0\"},{\"fileId\":10000088060171,\"fileName\":\"ui.jpg\",\"uploaderId\":\"WU_FILE_1\"}]}";
        JSONObject jsonObject=JSONObject.fromObject(result);
        JSONArray jsonArray = null;
        try {
            jsonArray = new JSONArray(jsonObject.getString("attachs"));
            for(int i=0;i<jsonArray.length();i++){      
                Object object = jsonArray.get(i);
                jsonObject=JSONObject.fromObject(object.toString());
                System.out.println(jsonObject.get("fileId"));
            }
            } catch (Exception e) {
                e.printStackTrace();
            }
    }
输出结果:
10000088060170
10000088060171
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
【社区内容提示】社区部分内容疑似由AI辅助生成,浏览时请结合常识与多方信息审慎甄别。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

相关阅读更多精彩内容

友情链接更多精彩内容