map 返回值json 是对象的情况下,取属性值

        Person p = new Person();
        p.setId("1");
        p.setAge(20);
        p.setUserName("a");

        Map<String,String> map = new HashMap<>();
        map.put("code","0");
        map.put("msg","成功");
        map.put("result","{\n" + "  \"id\": \"74233\",\n" + "  \"userName\": \"P001219\",\n" +  "  \"age\": \"20\",\n" +
                "  \"address\": \"1002975101\",\n" +
                "  \"brithday\": \"2012-12-12\"\n" +
                "}");
        String result = map.get("result");
        //方案一:alibaba.fastjson
       /* System.out.println("结果:"+JSONObject.toJSON(p));
        Person ps =  JSONObject.parseObject(result,Person.class);
        System.out.println("转换后得到的结果:"+ps.getUserName());*/
        //方案二:net.sf.json
        JSONObject jsonObject = JSONObject.fromObject(result);
        String id = jsonObject.get("id").toString();
        System.out.println("得到的结果:"+id);

猜你喜欢

转载自blog.csdn.net/qq_35781178/article/details/81062787
今日推荐