Json解析方式之org.json的使用

解析原理

基于文件驱动,需要把全部文件读入到内存中,然后遍历所有数据,根据需要检索想要的数据

代码实例

	   public class OrgJson {
	
	
	    static  class Student{
	        private String name;
	        private String sex;
	        private int    age;
	
	        public List<Course> getList() {
	            return list;
	        }
	
	        public void setList(List<Course> list) {
	            this.list = list;
	        }
	
	        private List<Course> list;
	
	
	
	        public String getName() {
	            return name;
	        }
	
	        public void setName(String name) {
	            this.name = name;
	        }
	
	        public String getSex() {
	            return sex;
	        }
	
	        public void setSex(String sex) {
	            this.sex = sex;
	        }
	
	        public int getAge() {
	            return age;
	        }
	
	        public void setAge(int age) {
	            this.age = age;
	        }
	
	        @Override
	        public String toString() {
	            return "Student{" +
	                    "name='" + name + '\'' +
	                    ", sex='" + sex + '\'' +
	                    ", age=" + age +
	                    '}';
	        }
	
	        static class Course{
	
	            private String  name;
	
	            private float score;
	
	            public Course() {
	            }
	
	            public String getName() {
	                return name;
	            }
	
	            public void setName(String name) {
	                this.name = name;
	            }
	
	            public float getScore() {
	                return score;
	            }
	
	            public void setScore(float score) {
	                this.score = score;
	            }
	
	            @Override
	            public String toString() {
	                return "Course{" +
	                        "name='" + name + '\'' +
	                        ", score=" + score +
	                        '}';
	            }
	        }
	
	    }
	
	
	    /**
	     * 生成一个json字符串
	     * @param context
	     */
	    public static void  createJson(Context context) throws Exception {
	        //获取json文件
	        File  file=new File(context.getCacheDir(),"orgjson.json");
	
	        JSONObject student=new JSONObject();
	        student.put("name","orgJson");
	        student.put("sax","男");
	        student.put("age",23);
	
	        JSONObject course1=new JSONObject();
	        course1.put("name","语文");
	        course1.put("score",98.5f);
	
	        JSONObject course2=new JSONObject();
	        course2.put("name","数学");
	        course2.put("score",95.5f);
	
	        //将course1,course2放入数组中
	        JSONArray courses=new JSONArray();
	        courses.put(0,course1);
	        courses.put(1,course2);
	
	
	        student.put("courses",courses);
	
	        FileOutputStream fos=new FileOutputStream(file);
	        fos.write(student.toString().getBytes());
	        fos.close();
	        System.out.println("createJson:"+student.toString());
	    }
	
	
	    /**
	     * 解析json
	     * @param context
	     */
	    public static  void parseJson(Context context) throws Exception {
	        File  file=new File(context.getCacheDir(),"orgjson.json");
	
	        FileInputStream fos=new FileInputStream(file);
	
	        InputStreamReader isr=new InputStreamReader(fos);
	
	        BufferedReader  br=new BufferedReader(isr);
	
	        StringBuffer stringBuffer=new StringBuffer();
	        String line;
	
	        while(null!=(line=br.readLine())){
	            stringBuffer.append(line);
	        }
	        fos.close();
	        isr.close();
	        br.close();
	
	        System.out.println("原始json数据:"+stringBuffer.toString());
	        Student bean=new Student();
	
	        JSONObject jsonObject=new JSONObject(stringBuffer.toString());
	
	        String  name=jsonObject.optString("name","");
	        String  sex=jsonObject.optString("sex","男");
	        int     age=jsonObject.optInt("age",18);
	        bean.setName(name);
	        bean.setSex(sex);
	        bean.setAge(age);
	
	        //getString  若没有获取到对应字段的值时会抛错
	        //optString  若没有获取到对应字段的值是会返回""
	        List<Student.Course> list=new ArrayList<>();
	        JSONArray array = jsonObject.optJSONArray("courses");
	        for (int i=0;i<array.length();i++){
	             JSONObject object=array.getJSONObject(i);
	             String  name1=object.optString("name","");
	             double  score1=object.optDouble("score",0.0);
	             Student.Course course=new Student.Course();
	             course.setName(name1);
	             course.setScore((float) score1);
	             list.add(course);
	        }
	
	        bean.setList(list);
	
	        System.out.println("parseJson:"+bean.toString());

	    }
	   
    	}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值