Json的学习

   json的简介

            Json是项目中常用的一种,数据格式简单,易于读写,格式都是压缩的,占用带宽小,轻量级,支持多种语言,可以直接为服务器代码使用。

  json常用支持的转化,(map集合,字符串,还有对象)

         1   map集合转为json数据

	//将map集合转化为json
	public static void test1(){
		
		Map<String,String> map=new HashMap<>();
		
		map.put("name", "zhangsan");
		map.put("age", "23");
		map.put("nickname", "xiaozhang");
		
		JSONObject jsonObject=new JSONObject(map);
		System.out.println(jsonObject);
		
		
	}

         2.对象转为json数据

//    将对象转化为json
    public static void test2(){
        
        Student student=new Student(1001,"张三",23);
        
        JSONObject jsonObject=new JSONObject(student);
        
        System.out.println(jsonObject);
    }

      3.字符串转为json

//    将字符串转为json
    public static void test3(){
        
        String str="{\"name\":\"李四\",\"age\":23,\"clas\":\"后台班\"}";
        
        JSONObject jsonObject=new JSONObject(str);
        
        System.out.println(jsonObject);
    }

二. 将json文件转化输出到控制台,也可以使用commons.io这个工具

		InputStream in=super.getClass().getClassLoader().getResourceAsStream("test/stu.json");
//		
//		byte[] bs=new byte[1024];
//		
//		int len=-1;
//		StringBuffer sb=new StringBuffer();
//		while((len=in.read(bs))!=-1){
////		byte-->string
//			String str=new String(bs,0,len);
//			sb.append(str);
//	
//		}
////		Stringbuffer-->json
//		String s=sb.toString();

 使用commons.io一句话就可以实现将json文件转为string

      String str=FileUtils.readFileToString(new File("E:\\eclipseWorkspace\\test\\src\\test\\stu.json"));

三、使用Java代码可以生成一个json文件

     

// 生成json文件
    public void test5() throws IOException{
        Map<String, Student> map=new HashMap<>();
    
        Student stu1=new Student(1001,"张三",23);
        Student stu2=new Student(1002,"李四",24);
        Student stu3=new Student(1003,"王五",25);

        map.put("lxl", stu1);
        map.put("zxl", stu2);
        map.put("wxl", stu3);
//        map-->json
        JSONObject jsonObject=new JSONObject(map);
//        生成json文件
     Writer writer    =new FileWriter("E:\\eclipseWorkspace\\test\\src\\test\\student.json");

     jsonObject.write(writer);
     writer.close();    
    }

四、jsonArray的介绍(json数组)

一般形如  [{...}, {....},{....}]此种类型称为json数组

   四、1 可以将字符串转为json数组

  

    public void test6(){
        String str=
                "[{\"name\":\"张三\",\"age\":23},{\"class\":\"lxl\",\"num\":12},{\"school\":\"sun\",\"addr\":\"sh\"}]";

      net.sf.json.JSONArray json=new net.sf.json.JSONArray();
      
      json=json.fromObject(str);
        System.out.println(json);
        
    }

  四 2  json-lib包的使用,导入jar包

        核心代码:JsonArray json=new JsonArray;

                              json =fromObject(Object o);

        实例代码   1.将map集合转为json数组

    public void test7(){
        Map<String, Student> map=new HashMap<>();
        
        Student stu1=new Student(1001,"张三",23);
        Student stu2=new Student(1002,"李四",24);
        Student stu3=new Student(1003,"王五",25);

        map.put("lxl", stu1);
        map.put("zxl", stu2);
        map.put("wxl", stu3);
        
          net.sf.json.JSONArray json=new net.sf.json.JSONArray();
          
          json=json.fromObject(map);
            System.out.println(json);
    }

     五、使用jsonArray转为map

思路:先从jsonArray中获取每一个json,获取json的键值对,json.get(i)获取每一个json,强转为jsonObject对象,jsonObject.keySet()可以获取json的所有的键的值,jsonObject.get(key)获取value的值,map.put重新装入map集合中。

    public void test8(){
        String str=
                "[{\"name\":\"张三\",\"age\":23},{\"class\":\"lxl\",\"num\":12},{\"school\":\"sun\",\"addr\":\"sh\"}]";
         net.sf.json.JSONArray json=new net.sf.json.JSONArray();
          
          json=json.fromObject(str);
//          从jsonarray中获取每一个json
          for (int i=0; i<json.size() ;i++) {
              Object o=json.get(i);//获取每一个json
              net.sf.json.JSONObject jsonObject=(net.sf.json.JSONObject) o;
//            获取json的key/value
              Map<String, Object> map=new HashMap<>();
            Set<String> keys=  jsonObject.keySet();//每个json的所有的key
            for (String key : keys) {
                  Object value=jsonObject.get(key);
                   map.put(key, value);
            }
            
            System.out.println(map);
            
            
        }

 

posted @ 2019-01-16 22:52  islandlxl  阅读(218)  评论(0编辑  收藏  举报