JSONObject操作案例

Posted 薛柏梁

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了JSONObject操作案例相关的知识,希望对你有一定的参考价值。

JSONObject jsonObject1 =JSONObject.parseObject(String)
@RequestMapping(params="getICInfo")
    @ResponseBody
    @ApiOperation(value="IC接口",produces="application/json",httpMethod="POST")
    public  JSONObject getICInfo(String token, String data) {
        JSONObject jsonObject =new JSONObject();
        JSONArray jsonarray = new JSONArray();  
        Date date = new Date();
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");  
        String sql = "";
        try {  
                if(StringUtils.isNotBlank(data)){
                    if("999".equals(data)){
                        sql = " select a.*,b.cus_company ,c.realname "
                                + "   from ld_ic_info a "
                                + "   left join ld_customer b on (a.ic_fromcompany = b.id) "
                                + "   left join t_s_base_user c on (a.ic_person = c.id ) "
                                + "  where update_state > 0 and ic_state in(‘3‘,‘4‘)";
                        List<Map<String, Object>> queryForList = jdbcTemplate.queryForList(sql);
                        if(queryForList!=null && queryForList.size()>0) {
                            
                            for (Map<String, Object> map : queryForList) { 
                                JSONObject json =new JSONObject(); 
                                String ic_code = "";
                                String cus_company = "";
                                String ic_bill_code = "";
                                String ic_comment = "";
                                String ic_pricecomment = "";
                                String ic_invoicecomment = "";
                                String ic_person = "";
                                String ic_ticket = "";
                                String ic_type = "";
                                String ic_rutime = "";
                                String ic_firstrutime = "";
                                String ic_Qyperson = "";
                                String ic_Qyperson_Phone = "";
                                String update_state = "";
                                String create_by = "";
                                String create_date = "";
                                String update_by = "";
                                String update_date = "";
                                if(map.get("ic_code")!=null){
                                    ic_code = String.valueOf(map.get("ic_code"));
                                }
                                if(map.get("cus_company")!=null){
                                    cus_company = String.valueOf(map.get("cus_company"));
                                }
                                if(map.get("ic_bill_code")!=null){
                                    ic_bill_code = String.valueOf(map.get("ic_bill_code"));
                                }
                                if(map.get("ic_comment")!=null){
                                    ic_comment = String.valueOf(map.get("ic_comment"));
                                }
                                if(map.get("ic_pricecomment")!=null){
                                    ic_pricecomment = String.valueOf(map.get("ic_pricecomment"));
                                }
                                if(map.get("ic_invoicecomment")!=null){
                                    ic_invoicecomment = String.valueOf(map.get("ic_invoicecomment"));
                                }
                                if(map.get("realname")!=null){
                                    ic_person = String.valueOf(map.get("realname"));
                                }
                                if(map.get("ic_ticket")!=null){
                                    ic_ticket = String.valueOf(map.get("ic_ticket"));
                                }
                                if(map.get("ic_type")!=null){
                                    ic_type = String.valueOf(map.get("ic_type"));
                                }
                                if(map.get("ic_rutime")!=null){
                                    ic_rutime = sdf.format(map.get("ic_rutime"));
                                } 
                                if(map.get("ic_firstrutime")!=null){
                                    ic_firstrutime = sdf.format(map.get("ic_firstrutime"));
                                }
                                if(map.get("ic_Qyperson")!=null){
                                    ic_Qyperson = String.valueOf(map.get("ic_Qyperson"));
                                }
                                if(map.get("ic_Qyperson_Phone")!=null){
                                    ic_Qyperson_Phone = String.valueOf(map.get("ic_Qyperson_Phone"));
                                }
                                if(map.get("update_state")!=null){
                                    update_state = String.valueOf(map.get("update_state"));
                                }
                                if(map.get("create_by")!=null){
                                    create_by = String.valueOf(map.get("create_by"));
                                }
                                if(map.get("create_date")!=null){
                                    create_date = sdf.format(map.get("create_date"));
                                }
                                if(map.get("update_by")!=null){
                                    update_by = String.valueOf(map.get("update_by"));
                                }
                                if(map.get("update_date")!=null){
                                    update_date = sdf.format(map.get("update_date"));
                                }
                                json.put("ic_code", ic_code);
                                json.put("ic_fromcompany", cus_company);
                                json.put("ic_bill_code", ic_bill_code);
                                json.put("ic_comment", ic_comment);
                                json.put("ic_pricecomment", ic_pricecomment);
                                
                                json.put("ic_invoicecomment", ic_invoicecomment);
                                json.put("ic_person", ic_person);
                                json.put("ic_ticket", ic_ticket);
                                json.put("ic_type", ic_type);
                                json.put("ic_rutime", ic_rutime);
                                
                                json.put("ic_firstrutime", ic_firstrutime);
                                json.put("ic_Qyperson", ic_Qyperson);
                                json.put("ic_Qyperson_Phone", ic_Qyperson_Phone);
                                json.put("updatestate", update_state);
                                json.put("create_by", create_by);
                                 
                                json.put("create_date", create_date);
                                json.put("update_by",update_by);
                                json.put("update_date", update_date);
                                
                                String ic_id = String.valueOf(map.get("id"));
                                sql = " select a.ic_type,a.ic_cost,a.ic_offer from ld_ic_price a ,ld_ic_info b where a.ic_id = b.id and b.id=‘"+ic_id+"‘";
                                List<Map<String, Object>> sonList = jdbcTemplate.queryForList(sql);
                                JSONArray jsonarray1 = new JSONArray();  
                                for (Map<String, Object> map2 : sonList) { 
                                    JSONObject json1 =new JSONObject();
                                    String ic_type1 = String.valueOf(map2.get("ic_type"));
                                    String ic_cost = String.valueOf(map2.get("ic_cost"));
                                    String ic_offer = String.valueOf(map2.get("ic_offer"));
                                    if(map2.get("ic_cost")==null){
                                        ic_cost="";
                                    }
                                    if(map2.get("ic_offer")==null){
                                        ic_offer="";
                                    }
                                    if(StringUtils.isNotBlank(ic_type1)){
                                        json1.put("ic_type", ic_type1);
                                        json1.put("ic_cost", ic_cost);
                                        json1.put("ic_offer", ic_offer);
                                        jsonarray1.add(json1);
                                    }
                                }
                                json.put("fy_info",JSONObject.toJSONString(jsonarray1));        
                                jsonarray.add(json);
                            }
                        } 
                    }else{
                        sql = " select a.*,b.cus_company ,c.realname "
                                + "   from ld_ic_info a "
                                + "   left join ld_customer b on (a.ic_fromcompany = b.id) "
                                + "   left join t_s_base_user c on (a.ic_person = c.id ) "
                                + "  where a.ic_code = ‘"+data+"‘  ";
                        Map<String, Object> map  = jdbcTemplate.queryForMap(sql);
                        if(map!=null && map.size()>0) {
                            JSONObject json =new JSONObject(); 
                            String ic_code = "";
                            String cus_company = "";
                            String ic_bill_code = "";
                            String ic_comment = "";
                            String ic_pricecomment = "";
                            String ic_invoicecomment = "";
                            String ic_person = "";
                            String ic_ticket = "";
                            String ic_type = "";
                            String ic_rutime = "";
                            String ic_firstrutime = "";
                            String ic_Qyperson = "";
                            String ic_Qyperson_Phone = "";
                            String update_state = "";
                            String create_by = "";
                            String create_date = "";
                            String update_by = "";
                            String update_date = "";
                            if(map.get("ic_code")!=null){
                                ic_code = String.valueOf(map.get("ic_code"));
                            }
                            if(map.get("cus_company")!=null){
                                cus_company = String.valueOf(map.get("cus_company"));
                            }
                            if(map.get("ic_bill_code")!=null){
                                ic_bill_code = String.valueOf(map.get("ic_bill_code"));
                            }
                            if(map.get("ic_comment")!=null){
                                ic_comment = String.valueOf(map.get("ic_comment"));
                            }
                            if(map.get("ic_pricecomment")!=null){
                                ic_pricecomment = String.valueOf(map.get("ic_pricecomment"));
                            }
                            if(map.get("ic_invoicecomment")!=null){
                                ic_invoicecomment = String.valueOf(map.get("ic_invoicecomment"));
                            }
                            if(map.get("realname")!=null){
                                ic_person = String.valueOf(map.get("realname"));
                            }
                            if(map.get("ic_ticket")!=null){
                                ic_ticket = String.valueOf(map.get("ic_ticket"));
                            }
                            if(map.get("ic_type")!=null){
                                ic_type = String.valueOf(map.get("ic_type"));
                            }
                            if(map.get("ic_rutime")!=null){
                                ic_rutime = sdf.format(map.get("ic_rutime"));
                            } 
                            if(map.get("ic_firstrutime")!=null){
                                ic_firstrutime = sdf.format(map.get("ic_firstrutime"));
                            }
                            if(map.get("ic_Qyperson")!=null){
                                ic_Qyperson = String.valueOf(map.get("ic_Qyperson"));
                            }
                            if(map.get("ic_Qyperson_Phone")!=null){
                                ic_Qyperson_Phone = String.valueOf(map.get("ic_Qyperson_Phone"));
                            }
                            if(map.get("update_state")!=null){
                                update_state = String.valueOf(map.get("update_state"));
                            }
                            if(map.get("create_by")!=null){
                                create_by = String.valueOf(map.get("create_by"));
                            }
                            if(map.get("create_date")!=null){
                                create_date = sdf.format(map.get("create_date"));
                            }
                            if(map.get("update_by")!=null){
                                update_by = String.valueOf(map.get("update_by"));
                            }
                            if(map.get("update_date")!=null){
                                update_date = sdf.format(map.get("update_date"));
                            }
                            json.put("ic_code", ic_code);
                            json.put("ic_fromcompany", cus_company);
                            json.put("ic_bill_code", ic_bill_code);
                            json.put("ic_comment", ic_comment);
                            json.put("ic_pricecomment", ic_pricecomment);
                            
                            json.put("ic_invoicecomment", ic_invoicecomment);
                            json.put("ic_person", ic_person);
                            json.put("ic_ticket", ic_ticket);
                            json.put("ic_type", ic_type);
                            json.put("ic_rutime", ic_rutime);
                            
                            json.put("ic_firstrutime", ic_firstrutime);
                            json.put("ic_Qyperson", ic_Qyperson);
                            json.put("ic_Qyperson_Phone", ic_Qyperson_Phone);
                            json.put("updatestate", update_state);
                            json.put("create_by", create_by);
                             
                            json.put("create_date", create_date);
                            json.put("update_by",update_by);
                            json.put("update_date", update_date);
                            String ic_id = String.valueOf(map.get("id"));
                            sql = " select a.ic_type,a.ic_cost,a.ic_offer from ld_ic_price a ,ld_ic_info b where a.ic_id = b.id and b.id=‘"+ic_id+"‘";
                            List<Map<String, Object>> sonList = jdbcTemplate.queryForList(sql);
                            JSONArray jsonarray1 = new JSONArray();  
                            for (Map<String, Object> map2 : sonList) { 
                                JSONObject json1 =new JSONObject();
                                String ic_type1 = String.valueOf(map2.get("ic_type"));
                                String ic_cost = String.valueOf(map2.get("ic_cost"));
                                String ic_offer = String.valueOf(map2.get("ic_offer"));
                                if(map2.get("ic_cost")==null){
                                    ic_cost="";
                                }
                                if(map2.get("ic_offer")==null){
                                    ic_offer="";
                                }
                                if(StringUtils.isNotBlank(ic_type1)){
                                    json1.put("ic_type", ic_type1);
                                    json1.put("ic_cost", ic_cost);
                                    json1.put("ic_offer", ic_offer);
                                    jsonarray1.add(json1);
                                }
                            }
                            json.put("fy_info",JSONObject.toJSONString(jsonarray1));        
                            jsonarray.add(json);
                        }
                    }
                } 
            
            jsonObject.put("content", jsonarray);
            jsonObject.put("result", "success");
            systemService.addLog("执行客户付款周期诚信数据同步", Globals.Log_Type_ldcorder, Globals.Log_Leavel_INFO);
    
            
        } catch (Exception e) {
            
            e.printStackTrace();
            jsonObject.put("result", "error");
        }
        
        return jsonObject;
    }


    @RequestMapping(params="setICUpdtaestate")
    @ResponseBody
    @ApiOperation(value="调用成功的数据返回设置update_state",produces="application/json",httpMethod="POST")
    public  JSONObject setICUpdtaestate(String token, String content) {
        JSONObject jsonObject =new JSONObject();
        List<Map> parseArray = JSONObject.parseArray(content, Map.class);
         
        try {
            for (Map map : parseArray) {
                
                String ic_code = (String) map.get("ordercode");    
                String update_state = (String) map.get("updatestate");     
                
                if(StringUtils.isNotEmpty(ic_code)) {
                     String sqldem=" update  ld_ic_info a set a.update_state = 0 where a.ic_code = ‘"+ic_code+"‘ and a.update_state = ‘"+update_state+"‘";
                     jdbcTemplate.execute(sqldem);    
                }  
            }
            jsonObject.put("result", "success");
        } catch (Exception e) {
            
            e.printStackTrace();
            jsonObject.put("result", "error");
        }
        
        return jsonObject;
    }

 

以上是关于JSONObject操作案例的主要内容,如果未能解决你的问题,请参考以下文章

高效 告别996,开启java高效编程之门 3-23汇总操作实战案例

高效 告别996,开启java高效编程之门 3-23汇总操作实战案例

如何在 jsonobject 中设置数组值

FastJson的常用操作

Express实战 - 应用案例- realworld-API - 路由设计 - mongoose - 数据验证 - 密码加密 - 登录接口 - 身份认证 - token - 增删改查API(代码片段

FastJson的常用操作