FastJson의 기본 사용 사례

4081 단어

JSON 문자열로 객체 정렬하기

        User user = new User();
        user.setName(" ");
        user.setAge(12);
        user.setEmail("[email protected]");

        String formarStr = JSON.toJSONString(user);
        KLog.d(" --", formarStr);

         
        
        {"age":12,"email":"[email protected]","name":" "}


JSON 문자열을 정렬하여 객체로 만들기

        String jsonString = "{\"name\":\" \",\"age\":20,\"email\":\"[email protected]\"}";
        User user1 = JSON.parseObject(jsonString,User.class);
        KLog.d(" --", user1.toString());

         
        
        User{name=' ', age=20, email='[email protected]'}


JSON 문자열로 배열

        List users = new ArrayList<>();
        users.add(user);
        users.add(user1);
        String userStr = JSON.toJSONString(users);
        KLog.d(" -userStr-", userStr);
        
         
        
        [{"age":12,"email":"[email protected]","name":" "},{"age":20,"email":"[email protected]","name":" "}]
        

JSON 문자열 배열화

        List user2 = JSON.parseArray(userStr,User.class);
        KLog.d(" -user2.size()-", user2.size());
        
         
        
        2


JSON 문자열로 정렬된 맵

        Map dataMap = new HashMap<>();
        dataMap.put("tag", "data");
        dataMap.put("hospitalName", " ");
        dataMap.put("name", " ");
        dataMap.put("gender", " ");
        dataMap.put("age", "28");
        dataMap.put("code", "420502042");
        dataMap.put("examineDoc", " ");
        dataMap.put("examineDate", "2016-05-10");
        dataMap.put("verifyDoc", " ");
        KLog.d("MapToJson-", JSON.toJSONString(dataMap));
        
         
        
         {"age":"28","code":"420502042","examineDate":"2016-05-10","examineDoc":" ","gender":" ","hospitalName":" ","name":" ","tag":"data","verifyDoc":" "}


JSON 문자열을 맵으로 역정렬하기

        Map dataMap = new HashMap<>();
        dataMap.put("tag", "data");
        dataMap.put("hospitalName", " ");
        dataMap.put("name", " ");
        dataMap.put("gender", " ");
        dataMap.put("age", "28");
        dataMap.put("code", "420502042");
        dataMap.put("examineDoc", " ");
        dataMap.put("examineDate", "2016-05-10");
        dataMap.put("verifyDoc", " ");
        KLog.d("MapToJson-", JSON.toJSONString(dataMap));
        Map maps =  JSON.parseObject(JSON.toJSONString(dataMap),Map.class);
        KLog.d("JsonToMap-",maps.toString());
        
         
        Map maps =  JSON.parseObject(JSON.toJSONString(dataMap),Map.class);
        
         
        
         {age=28, tag=data, examineDate=2016-05-10, verifyDoc= , name= , gender= , examineDoc= , hospitalName= , code=420502042}

        

만약 어떤 속성만 해석하고 싶다면

        User user = new User();
        user.setName(" ");
        user.setAge(12);
        user.setEmail("[email protected]");
        SimplePropertyPreFilter filter = new SimplePropertyPreFilter(User.class, "name");
        String formarStr = JSON.toJSONString(user,filter);
        KLog.d(" --", formarStr);
        
         
        
        {"name":" "}
        

좋은 웹페이지 즐겨찾기