解析json案例

代码如下:

 public static void main(String[] args) {    
     analysisJson();
  }

   public static void analysisJson() {
    String jsonStr = "{\"errorCode\": \"0\",\"errorMsg\": \"调用接口成功\",\"data\": [{\"userName\": \"贺雷\",\"position\": \"网站技术负责人\",\"webAddres\": \"www.ithelei.com\"}]}";
    // 将json字符串转换成json
    JSONObject root = new JSONObject().parseObject(jsonStr);
    
    String errorCode = root.getString("errorCode");
    String errorMsg = root.getString("errorMsg");
    System.out.println("errorCode:" + errorCode + ",errorMsg:" + errorMsg);
    
    JSONArray dataArr = root.getJSONArray("data"); 
    for (int i = 0; i < dataArr.size(); i++) {
        JSONObject dataBean = (JSONObject) dataArr.get(i);
        String position = dataBean.getString("position");
        String userName = dataBean.getString("userName");
        String webAddres = dataBean.getString("webAddres");
        System.out.println("position:" + position + ",userName:" + userName + ",webAddres:" + webAddres);
    }
    
}

©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容