代码如下:
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);
}
}
- 邮箱 :ithelei@sina.cn
- Good Luck!