【Java开发】通过map实现双层json字符串嵌套的标准json字符串转换

{
   "touser" : "UserID1|UserID2|UserID3",
   "toparty" : "PartyID1|PartyID2",
   "totag" : "TagID1 | TagID2",
   "msgtype" : "text",
   "agentid" : 1,
   "text" : {
       "content" : "你的快递已到,请携带工卡前往邮件中心领取。\n出发前可查看<a href=\"http://work.weixin.qq.com\">邮件中心视频实况</a>,聪明避开排队。"
   },
   "safe":0,
   "enable_id_trans": 0,
   "enable_duplicate_check": 0,
   "duplicate_check_interval": 1800
}

  比如这种格式的json字符串,要拼接会非常烦,但是用自带的map转换json的方法会导致最后的json字符串出现格式问题,这种情况我们就可以用这种方法方法来规避问题。

具体代码参考:

  可能会有依赖要导入,如果报错就百度一下报错的地方,找一下依赖就行了,很简单的。

    public static String GetJson(String str1, Integer agentId,
                                 String name, String areaNum, String departmentNum, String bedNum, String phoneNum) {

        //str1为工号,agentId为应用号,name为患者姓名,areaNum为病区号,departmentNum为科室号,bedNum床位号,phoneNum为患者手机号
        Scanner scan = new Scanner(System.in);
        String str2 = "";          //str2为str1转变后的标准格式

        List<String> oriUser = Arrays.asList(str1.split(","));


        for (int i = 0; i < oriUser.size(); i++) {
            if (i != 0) {
                str2 = str2 + "|";
            }
            String s = oriUser.get(i);
            s.trim();
            str2 = str2 + s;
        }
        String userid = str2;


        Map<String, Object> map2 = new HashMap<String, Object>();
        String title = "床位预约成功";                  //消息标题
        String description = "";

        Date date = new Date();//获取当前的日期
        SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");//设置日期格式
        String time = df.format(date);//获取String类型的时间


        //信息主体
        description = description + "<div class=\"gray\">" + time + "</div>";
        description = description + "<div class=\"normal\">" + name + "</div>";
        description = description + "<div class=\"normal\">" + phoneNum + "</div>";
        description = description + "<div class=\"normal\">已安排至" + areaNum + "病区" + departmentNum + "科室" + bedNum + "床位</div>";
        description = description + "<div class=\"highlight\">" + "请联系患者尽快办理住院手续</div>";

        String url = "http://www.xxxxx.com";         //详情网址
        String btntxt = "";                   //按钮内容
        map2.put("title", title);
        map2.put("description", description);
        map2.put("url", url);
        map2.put("btntxt", btntxt);
        String mapJson2 = JSON.toJSONString(map2);    //先将text的内容转化为jason字符串

        Map<String, Object> map = new HashMap<String, Object>();
        map.put("touser", userid);
        map.put("msgtype", "textcard");
        map.put("agentid", agentId);
        map.put("textcard", mapJson2);

        String mapJson = JSON.toJSONString(map);   //map转换为json形式
        //json串格式化,成为普通的string串
        String newString = StringEscapeUtils.unescapeJavaScript(mapJson);   
        //但是普通的string串使用map<string,string>拼接,string两边会有双引号
        String s3 = newString.replace(":\"{", ":{");  
        //修改,把第二层json外面多余的双引号去除
        String ans = s3.replace("}\"", "}");
        return ans;//返回的ans为最终所需要的串
    }

  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

Why_so?

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值