java fastjson处理json

2017-04-21  本文已影响0人  橙小光
//json字符串变json对象
json对象 = JSON.parseObject(json字符串,json对象);
//json数组变json对象
LIST<json对象> xx =JSON.parseArray(json数组,json对象) ;
//json对象转json字符串
json字符串 = toJSONString(json对象);
 <!--fastjson-->
    <dependency>
      <groupId>com.alibaba</groupId>
      <artifactId>fastjson</artifactId>
      <version>1.1.41</version>
    </dependency>
/**
     * 批量置为有效
     * @Param
     * @Return
     * */
    @ResponseBody
    @RequestMapping(value = "/effectivemore", method = RequestMethod.POST)//POST
    public JsonResponse toEffectiveMore(@Validated String params) throws Exception{
        JsonResponse data = null;
        List<Effective> list = JSON.parseArray(params,Effective.class);
        int line = 0;
        for (Effective eff:list){
             line +=  editorResource.effective(eff);
        }
        if (line>=list.size()){
            data = new JsonResponse(ResponseCode.RESPONSE_SUCCESS, "置为有效成功", line);
        }else {
            data = new JsonResponse(ResponseCode.RESPONSE_ERROR, "置为有效失败", line);
        }
        return data;
    }

  String jsons = "[{\"f_catalog_id\":551,\"f_catalog_name\":\"科大讯飞海绵听书\",\"f_catalog_parentid\":0},{\"f_catalog_id\":552,\"f_catalog_name\":\"小说\",\"f_catalog_parentid\":551},{\"f_catalog_id\":553,\"f_catalog_name\":\"评书\",\"f_catalog_parentid\":551},{\"f_catalog_id\":554,\"f_catalog_name\":\"逻辑思维\",\"f_catalog_parentid\":551},{\"f_catalog_id\":558,\"f_catalog_name\":\"地道战\",\"f_catalog_parentid\":553}]";
        List<JsonClass> list = JSON.parseArray(jsons,JsonClass.class);
        //查找f_catalog_name为 “地道战” 的f_catalog_id 558
        for (JsonClass js:list){
            if(js.getF_catalog_name().equals("百团大战")){
                System.out.println(js.getF_catalog_id());
            }
        }
上一篇 下一篇

猜你喜欢

热点阅读