【问题标题】:Beautifying string to JSON when there is a JSON payload within a JSON wrapper当 JSON 包装器中有 JSON 有效负载时,将字符串美化为 JSON
【发布时间】:2019-12-16 09:30:55
【问题描述】:

我有一个由 JSON 包装器组成的 JSON 字符串,并且在这个包装器中是 JSON 有效负载。

我需要对字符串进行美化,使其更具可读性。

我尝试过使用一些常规的美化器(Newtonsoft,加上我在 SO 上找到的一个不错的美化器),但是由于这个 JSON 字符串提供给我的方式,它们不起作用

这是我的 JSON 字符串

{ "LocalReferenceNumber": "DNXLHR1906000000000005", "DeclarationStatus": "已提交", "有效载荷": "{\"DeclarationType\":null,\"AcceptanceDateUtc\":null,\"DeclarationUcr\":\"9GB949032610000-AI-000000031-ASH\",\"LocalReferenceNumber\":\"DNXLHR1906000000000005\ ",\"TraderReference\":\"AI-000000031-ASH\",\"Exporter\":{\"IdentificationNumber\":null,\"Name\":null,\"Street\":null,\ "City\":null,\"PostCode\":null,\"Country\":null},\"Importer\":{\"IdentificationNumber\":null,\"Name\":null,\"Street \":null,\"City\":null,\"PostCode\":null,\"Country\":null},\"Seller\":{\"IdentificationNumber\":null,\"Name\" :null,\"Street\":null,\"City\":null,\"PostCode\":null,\"Country\":null},\"Buyer\":{\"IdentificationNumber\":null ,\"Name\":null,\"Street\":null,\"City\":null,\"PostCode\":null,\"Country\":null},\"Declarant\":{\ "IdentificationNumber\":\"GB949032610000\",\"Name\":\"ASM (UK) LTD\",\"Street\":\"ASHFORD HOUSE\",\"City\":\"ASHFORD\ ",\"PostCode\":\"TW15 2TQ\",\"Country\":\"GB\"},\"Representative\":{\"IdentificationNumber\":null,\"Name\":null ,\"街道\":null,\"城市\":null,\"邮政编码\":null,\" Country\":null},\"Representation\":0,\"TransportTypeOnArrival\":null,\"TransportIdentityOnArrival\":null,\"BorderTransportMode\":4,\"TransportCountryAtBorder\":null,\"InlandTransportMode \":null,\"TotalPackages\":null,\"DispatchCountry\":null,\"DestinationCountry\":null,\"InvoiceCurrency\":null,\"InvoiceTotal\":null,\"DeliveryTerms\" :null,\"ExchangeRate\":null,\"NatureOfTransaction\":null,\"GoodsLocation\":{\"CountryCode\":null,\"Type\":null,\"Quantifier\":null, \"Identification\":null,\"Name\":null},\"FirstDeferment\":null,\"SecondDeferment\":null,\"WarehouseIdentity\":null,\"WarehouseType\":null,\ "SupervisingOffice\":null,\"AirportOfLoading\":null,\"MovementReferenceNumber\":null,\"AuthorisationHolders\":[],\"Containers\":[],\"Guarantees\":[], \"AdditionalFiscalReferences\":[],\"AdditionalSupplyChainActors\":[],\"PreviousDocuments\":[{\"Category\":\"Z\",\"Type\":\"DCR\", \"参考\":\"9GB949032610000-AI-000000031-ASH\",\"标识符\":null,\"Order\":1}],\"Items\":[{\"ItemNumber\": 1,\"德clarationUcr\":null,\"TraderReference\":null,\"CountryOfOrigin\":null,\"PreferentialCountryOfOrigin\":null,\"Preference\":null,\"Quota\":null,\"GoodsDescription\ ":\"DSAFASDFSA\",\"ItemPrice\":null,\"ItemCurrency\":null,\"StatisticalValue\":null,\"StatisticalValueCurrency\":null,\"NetMass\":null,\" GrossMass\":null,\"SupplementaryUnits\":null,\"ValuationMethod\":null,\"ValuationIndicators\":null,\"DispatchCountry\":null,\"DestinationCountry\":null,\"NatureOfTransaction\ ":null,\"Exporter\":{\"IdentificationNumber\":null,\"Name\":null,\"Street\":null,\"City\":null,\"PostCode\":null ,\"Country\":null},\"Seller\":{\"IdentificationNumber\":null,\"Name\":null,\"Street\":null,\"City\":null,\ "PostCode\":null,\"Country\":null},\"Buyer\":{\"IdentificationNumber\":null,\"Name\":null,\"Street\":null,\"City \":null,\"PostCode\":null,\"Country\":null},\"CustomsUnionAndStatisticsNumber\":null,\"CommodityCode\":null,\"ProcedureCode\":null,\"CommodityAdditionalCodes\ ":[],\"附加程序代码\":[],\"P reviousDocuments\":[],\"Containers\":[],\"Packages\":[],\"FiscalReferences\":[],\"SupplyChainActors\":[],\"AdditionsAndDeductions\":[ ],\"AdditionalInformation\":[],\"DocumentCertificateAuthorisationReferences\":[],\"TaxLines\":[]}],\"OtherAdditionsAndDeductions\":[]}", "SubmittingBy": "devbuilder", “提交日期”:“2019-06-14T09:08:42.788Z” }

如您所见,“有效负载”部分包含“JSON 包装器”中的 JSON 数据,我需要格式化整个字符串,使其看起来像格式化的 JSON 数据,即适当的缩进等。

【问题讨论】:

  • 你看到这个answer了吗?寻找“prettyprint”,也许有帮助。
  • 问题是即使 Payload 的内容在技术上是 JSON 格式,它实际上只是一个字符串值,就像任何其他值一样。您必须将 Payload 对象从 JSON 中提取到其自己的 json 对象中,对其进行美化,然后将其放回整个 json 中。
  • 外部 JSON 包装器是否有固定架构?
  • 外层 JSON 有固定模式

标签: c# json


【解决方案1】:

最终从字符串中剥离了有效负载:

string payloadPropertyName = "\"Payload\":\"";
        int startIndex = rawMessage.IndexOf(payloadPropertyName, StringComparison.Ordinal) + payloadPropertyName.Length;
        int endIndex = rawMessage.IndexOf("\",\"SubmittingBy\"", StringComparison.Ordinal);

        string payload = rawMessage.Substring(startIndex, endIndex - startIndex);
        payload = payload.Replace("\\\"", "\"");

        return Newtonsoft.Json.Linq.JToken.Parse(payload).ToString(Newtonsoft.Json.Formatting.Indented);

美化:Newtonsoft.Json.Linq.JToken.Parse(payload).ToString(Newtonsoft.Json.Formatting.Indented);

然后我使用 JsonDeserialize 将我的包装 JSON 数据反序列化为一个对象

jsonSerializer.JsonDeserialize<Wrapper>(rawMessage);


 internal class Wrapper
{
    public string LocalReferenceNumber { get; set; }

    public string DeclarationStatus { get; set; }

    public string SubmittingBy { get; set; }

    public DateTime SubmittedOn { get; set; }
}

最后我只是格式化了 Wrapper 数据:

本地参考号:DNXLHR1906000000000005

申报状态:已提交

提交者:开发者

提交时间:“2019-06-14 09:08:42

接下来是我的美化 JSON 负载

一种解决方法,但在我的情况下也同样有效。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2021-12-01
    • 2019-05-31
    • 1970-01-01
    • 2018-08-29
    • 1970-01-01
    • 2018-01-18
    • 2021-01-19
    • 2019-12-27
    相关资源
    最近更新 更多