C# 当JSON包装中存在JSON负载时,将字符串美化为JSON

C# 当JSON包装中存在JSON负载时,将字符串美化为JSON,c#,json,C#,Json,我有一个JSON字符串,它由JSON包装器组成,在这个包装器中是JSON负载 我需要美化字符串,使其更具可读性 我尝试过使用一些常规的美化工具(Newtonsoft,加上我在SO上找到的一个不错的美化工具),但是由于这个JSON字符串给我的方式,它们不起作用 这是我的JSON字符串 { “LocalReferenceNumber”:“DNXLHR190600000000005”, “DeclarationStatus”:“已提交”, “有效负载:“{\”“声明类型\”“:null,\”“接受日期

我有一个JSON字符串,它由JSON包装器组成,在这个包装器中是JSON负载

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

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

这是我的JSON字符串

{ “LocalReferenceNumber”:“DNXLHR190600000000005”, “DeclarationStatus”:“已提交”, “有效负载:“{\”“声明类型\”“:null,\”“接受日期UTC\”:null,\”“声明UCR\:\“9GB9490326160000-AI-0000000 31-ASH\”,“本地引用编号\”“DNXLHR190600000000005\”,\“贸易参考\”“AI-0000000 31-ASH\”,“出口商\”“识别号\”“null,\”“姓名\”“null,\”“街道\”“null,\”“城市\”“null,\”,“邮政编码\”“进口商\”“国家:\”\“:{IdentificationNumber\\”:null,\'Name\':null,\'Street\':null,\'City\':null,\'PostCode\':null,\'Country\':null,\'Selleration\':{'IdentificationNumber\':null,\'Street\':null,\'City\':null,\'PostCode\'null,\'Country\':null,\'Buyer\'IdentificationNumber\':null,\'Name\':null,\'Street\'PostCode\'Country\':null},\“声明人\\”:{“身份号码\\”:“GB949032610000\”,\“姓名\\”:“ASM(英国)有限公司\“,“街道\\”:“阿什福德之家\“,\“城市\\”:“邮政编码\\”:“TW15 2TQ\”,“国家\“:“GB\”,“代表人\\”:{“身份号码\\”:null,\“姓名\\”:null,\“街道\\”:null,:“城市\\”,“邮政编码\\”,“国家\\”,::“国家\\”\“TransportTypeOnArrival\”:null,“TransportIdentityYonArrival\”:null,“BorderTransportMode\”:4,“TransportCountryAtBorder\”:null,“InlandTransportMode\”:null,“TotalPackages\”:null,“DispatchCountry\”:null,“DestinationCountry\”:null,“InvoiceCurrency\”:null,“InvoiceTotal\”:null,“DeliveryTerms\”:null”,“ExchangeRate\”:null,“Transaction”性质\“:null,\'GoodsLocation\':{'CountryCode\':null,\'Type\':null,\'Quantifier\':null,\'Identification\':null,\'Name\':null},\'firstdeference\':null,\'WarehouseIdentity\':null,\'WarehouseType\':null,\'SupervisingOffice\':null,\'AirportOfLoading\':null,\'MovementReferenceNumber\':null,\'authorizationHolders\':[],'Containers\':[]“,”担保\“:[],”其他金融参考\“:[],”其他供应链接受者\“:[],”以前的文档\“:[{\”类别\“:”Z \“,”类型\“:”DCR \“,”参考\“:”9GB949032610000-AI-0000000 31-ASH \“,”标识符\“:null,\”订单\“:1}],”项目\“:[{\\\\\\\\\\\\\”:[{\\\\\\\\\”项目编号\“:1,\”声明UCR\:null,\”贸易参考“:null,\”\“PreferentialCountryForigin\”: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,\'Country\':null,\'Seller\':{'identicationnumber\':null,\'Name\'Name\'null,\'Street\'null,\'City\':null,\'PostCode\'null,\'Country\':null,\'sellentificationnumber:{':null,\'Name\':null,\'Street\':null,\'City\':null,\'PostCode\':null,\'Country\':null,\'CustomsUnionAndStatisticsNumber\':null,\'CommodityCode\':null,\'CommodityAdditionalCodes\':[],\'AdditionalProcedureCodes\':[],\'PreviousDocuments\':[],“Containers\':[],“Containers\”:[],“Packages\:[],“FiscalReferences\”:[],“supplychaitors:[],:[],“Supplychaitors”\“附加和扣减\]:[],“附加信息\]:[],\“文档认证授权参考\]:[],\“税目\]:[],\“其他附加和扣减\]:[], “提交人”:“devbuilder”, “提交人”:“2019-06-14T09:08:42.788Z” }


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

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

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; }
}
jsonSerializer.JsonDeserialize(rawMessage); 内部类包装器 { 公共字符串LocalReferenceNumber{get;set;} 公共字符串声明状态{get;set;} {get;set;}提交的公共字符串 公共日期时间提交{get;set;} }
最后,我刚刚格式化了包装器数据:

本地参考号:DNXLHR190600000000005

申报情况:已提交

提交人:devbuilder

提交日期:“2019-06-1409:08:42

然后是我美化的JSON负载


这是一个解决办法,但在我的情况下也同样有效。

你看到了吗?寻找“预打印”问题是,尽管有效负载的内容在技术上是JSON格式的,但它实际上只是一个字符串值,就像其他任何值一样。您必须将负载对象从JSON中拉出来,放入它自己的JSON对象中,修饰它,然后将它放回整个JSON中。外部JSON包装器有固定的模式吗?外部JSON有固定的模式