package com.common.test;
import org.json.JSONArray;
import org.json.JSONObject;
/**
* @Author
*/
public class Test {
public static void main(String []args){
String json="{\n" +
" \"content\": {\n" +
" \"classNo\": \"IOQXT\",\n" +
" \"className\": \"高中三班\",\n" +
" \"teacherMobile\": \"18788781335\",\n" +
" \"teacher\": \"Jack\"\n" +
" },\n" +
" \"result\": {\n" +
" \"users\": [\n" +
" {\n" +
" \"id\": 1,\n" +
" \"name\": \"LiMei\",\n" +
" \"sex\": \"girl\"\n" +
" },\n" +
" {\n" +
" \"id\": 2,\n" +
" \"name\": \"ZhaoBao\",\n" +
" \"sex\": \"boy\"\n" +
" }\n" +
" ]\n" +
" },\n" +
" \"code\": \"200\",\n" +
" \"errorMsg\": \"\"\n" +
"}";
//System.out.print(json);
if(json!=""&&!json.equals("")){
JSONObject obj = new JSONObject(json.toString());
System.out.println("返回Code:"+obj.getString("code"));
String teacher=obj.getJSONObject("content").getString("teacher");
String teacherMobile=obj.getJSONObject("content").getString("teacherMobile");
System.out.println("教师姓名:" + teacher + " 教师电话:" + teacherMobile);
JSONObject resultObj = obj.getJSONObject("result");
JSONArray jArr =new JSONArray(resultObj.get("users").toString());
for(int i=0;i<jArr.length();i++){
System.out.println(jArr.get(i));
}
}
}
}
Test类:解析Json
最新推荐文章于 2022-02-28 13:20:12 发布