Test类:解析Json

来源:互联网 发布:vivo软件 编辑:程序博客网 时间:2024/04/28 15:27
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));            }        }    }}
0 0
原创粉丝点击