如何处理JSON解析中的控制字符..?

问题描述 投票:0回答:1

我有一个包含 JSON 内容的 HTTP 响应, 基于某些条件,我想删除响应中特定节点的键和值,这基本上是通过进行 JSON 解析(我尝试使用 Boost::ptree 和 nlohmann 库), 通过这样做,控制字符也被删除,我怎样才能避免这个问题..?

我尝试解析它并删除需要删除的字段,但控制字符也随之被删除,因为它正在通过解析到ptree(其中不保留控制字符),一旦我写将修改后的 ptree 返回到字符串,控制字符不存在并且内容长度有很大差异

这是例子

{
  "firstName": "John",
  "lastName": "Doe",
  "dateOfBirth": "1985-07-15",
  "ssn": "123-45-6789",
  "address": {
    "streetAddress": "123 Main St",
    "city": "Anytown",
    "state": "CA",
    "postalCode": "12345"
  },
  "phoneNumbers": [
    {
      "type": "home",
      "number": "555-555-5555"
    },
    {
      "type": "work",
      "number": "555-555-5556"
    }
  ],
  "email": "[email protected]"
}

这是我得到的输出,
{"address":{"city":"Anytown","postalCode":"12345","state":"CA","streetAddress":"123 Main St"},"dateOfBirth":"1985-07- 15","电子邮件":"[电子邮件受保护]","firstName":"John","lastName":"Doe","phoneNumbers":[{"number":"555-555-5555", "type":"家"},{"number":"555-555-5556","type":"工作"}],"ssn":"123-45-6789"}

其内容长度减少为 305,而原始内容长度为 325。

c++ boost nlohmann-json boost-json
1个回答
0
投票

您所说的“控制字符”是无关紧要的空格。与例如XML,根据规范,它们不是 JSON 文档的一部分,因此没有 JSON 库实现 1 跟踪它们/能够保留它们。

但是,您的输入看起来非常标准,使用 2 个空格的缩进宽度“打印得很漂亮”。您也可以这样做,例如使用

nlohmann
:

#include <nlohmann/json.hpp>
void using_nlohmann() {
    // parse and serialize JSON, preserving whitespace
    auto j = nlohmann::json::parse(text);

    // remove SSN and phone numbers
    j.erase("ssn");
    j.erase("phoneNumbers");
    std::cout << j.dump(2) << std::endl;
}

打印:

{
  "address": {
    "city": "Anytown",
    "postalCode": "12345",
    "state": "CA",
    "streetAddress": "123 Main St"
  },
  "dateOfBirth": "1985-07-15",
  "email": "[email protected]",
  "firstName": "John",
  "lastName": "Doe"
}

这可能足以满足您的要求。

总结

如果您使用 JSON,则不应依赖于无关紧要的空格(或其他非标准扩展,例如注释)。 “内容长度存在很大差异”这一论点不应该与任何以 JSON 格式交换数据的应用程序相关,因为规范说存在没有差异

演示

实时编译器资源管理器

#include <iostream>

static auto text = R"({
  "firstName": "John",
  "lastName": "Doe",
  "dateOfBirth": "1985-07-15",
  "ssn": "123-45-6789",
  "address": {
    "streetAddress": "123 Main St",
    "city": "Anytown",
    "state": "CA",
    "postalCode": "12345"
  },
  "phoneNumbers": [
    {
      "type": "home",
      "number": "555-555-5555"
    },
    {
      "type": "work",
      "number": "555-555-5556"
    }
  ],
  "email": "[email protected]"
})";

#include <nlohmann/json.hpp>
void using_nlohmann() {
    // parse and serialize JSON, preserving whitespace
    auto j = nlohmann::json::parse(text);

    // remove SSN and phone numbers
    j.erase("ssn");
    j.erase("phoneNumbers");
    std::cout << j.dump(2) << std::endl;
}

// the same using Boost.JSON
#include <boost/json.hpp>
#include <boost/json/src.hpp> // for compiler explorer
void using_boost() {
    auto j = boost::json::parse(text);

    // remove SSN and phone numbers
    j.as_object().erase("ssn");
    j.as_object().erase("phoneNumbers");
    std::cout << j << std::endl; // no built-in way to pretty-print
}

int main(){
    using_nlohmann();
    using_boost();
}

印刷

{
  "address": {
    "city": "Anytown",
    "postalCode": "12345",
    "state": "CA",
    "streetAddress": "123 Main St"
  },
  "dateOfBirth": "1985-07-15",
  "email": "[email protected]",
  "firstName": "John",
  "lastName": "Doe"
}
{"firstName":"John","lastName":"Doe","dateOfBirth":"1985-07-15","email":"[email protected]","address":{"streetAdd
ress":"123 Main St","city":"Anytown","state":"CA","postalCode":"12345"}}

¹ 据我所知

© www.soinside.com 2019 - 2024. All rights reserved.