在这里插入图片描述

一.你需要准备

  • jdk1.7
  • fastjson-1.1.33

二.我们要做什么事

1.处理前报文
{"1":"1","2":"2","3":"3","4":"4","smap":{"5":5,"6":"6","7":"7"}}
2.处理后报文
{"1":"1","2":"2","3":"3","4":"4","smap":"{\"5\":5,\"6\":\"6\",\"7\":\"7\"}"}

我们对比发现,在json报文中,我们给smap对象中的双引号都加了转义字符"\"

三.相关代码

1.处理前报文代码
import com.alibaba.fastjson.JSONObject;

import java.util.LinkedHashMap;

/**
 * @author: Milogenius
 * @create: 2019-08-02 16:57
 * @description:
 **/
public class Test {
    public static void main(String[] args) {
        LinkedHashMap<String, Object> fmap = new LinkedHashMap<>();
        fmap.put("1", "1");
        fmap.put("2", "2");
        fmap.put("3", "3");
        fmap.put("4", "4");
        LinkedHashMap<String, Object> smap = new LinkedHashMap<>();
        smap.put("5", 5);
        smap.put("6","6");
        smap.put("7","7");
        fmap.put("smap", smap);
        System.out.println(JSONObject.toJSONString(fmap));
    }
}
2.处理后报文代码
import com.alibaba.fastjson.JSONObject;

import java.util.LinkedHashMap;

/**
 * @author: Milogenius
 * @create: 2019-08-02 16:57
 * @description:
 **/
public class Test {
    public static void main(String[] args) {
        LinkedHashMap<String, Object> fmap = new LinkedHashMap<>();
        fmap.put("1", "1");
        fmap.put("2", "2");
        fmap.put("3", "3");
        fmap.put("4", "4");
        LinkedHashMap<String, Object> smap = new LinkedHashMap<>();
        smap.put("5", 5);
        smap.put("6","6");
        smap.put("7","7");
        fmap.put("smap", JSONObject.toJSONString(smap));
        System.out.println(JSONObject.toJSONString(fmap));
    }
}

对比两段代码,我们发现只有 fmap.put("smap", JSONObject.toJSONString(smap))这句代码不一样,的确,我们也是通过这句代码实现了上面的加转义字符的效果;

四.总结

我们通过使用fastjson工具包,实现了给json报文中特定对象加转义字符的要求,当然也有其他的实现思路,大家可以探索;

GitHub 加速计划 / js / json
18
5
下载
适用于现代 C++ 的 JSON。
最近提交(Master分支:3 个月前 )
f06604fc * :page_facing_up: bump the copyright years Signed-off-by: Niels Lohmann <mail@nlohmann.me> * :page_facing_up: bump the copyright years Signed-off-by: Niels Lohmann <mail@nlohmann.me> * :page_facing_up: bump the copyright years Signed-off-by: Niels Lohmann <niels.lohmann@gmail.com> --------- Signed-off-by: Niels Lohmann <mail@nlohmann.me> Signed-off-by: Niels Lohmann <niels.lohmann@gmail.com> 3 天前
d23291ba * add a ci step for Json_Diagnostic_Positions Signed-off-by: Harinath Nampally <harinath922@gmail.com> * Update ci.cmake to address review comments Signed-off-by: Harinath Nampally <harinath922@gmail.com> * address review comment Signed-off-by: Harinath Nampally <harinath922@gmail.com> * fix typo in the comment Signed-off-by: Harinath Nampally <harinath922@gmail.com> * fix typos in ci.cmake Signed-off-by: Harinath Nampally <harinath922@gmail.com> * invoke the new ci step from ubuntu.yml Signed-off-by: Harinath Nampally <harinath922@gmail.com> * issue4561 - use diagnostic positions for exceptions Signed-off-by: Harinath Nampally <harinath922@gmail.com> * fix ci_test_documentation check Signed-off-by: Harinath Nampally <harinath922@gmail.com> * address review comments Signed-off-by: Harinath Nampally <harinath922@gmail.com> * fix ci check failures for unit-diagnostic-postions.cpp Signed-off-by: Harinath Nampally <harinath922@gmail.com> * improvements based on review comments Signed-off-by: Harinath Nampally <harinath922@gmail.com> * fix const correctness string Signed-off-by: Harinath Nampally <harinath922@gmail.com> * further refinements based on reviews Signed-off-by: Harinath Nampally <harinath922@gmail.com> * add one more test case for full coverage Signed-off-by: Harinath Nampally <harinath922@gmail.com> * ci check fix - add const Signed-off-by: Harinath Nampally <harinath922@gmail.com> * add unit tests for json_diagnostic_postions only Signed-off-by: Harinath Nampally <harinath922@gmail.com> * fix ci_test_diagnostics Signed-off-by: Harinath Nampally <harinath922@gmail.com> * fix ci_test_build_documentation check Signed-off-by: Harinath Nampally <harinath922@gmail.com> --------- Signed-off-by: Harinath Nampally <harinath922@gmail.com> 3 天前
Logo

旨在为数千万中国开发者提供一个无缝且高效的云端环境,以支持学习、使用和贡献开源项目。

更多推荐