我有一个由JSON包装器组成的JSON字符串,在这个包装器中是JSON有效负载。
我需要美化字符串,使其更具可读性。
我试过使用一些常规的美化工具(Newtonsoft,外加我在SO上找到的一个很好的),但是由于这个JSON字符串被提供给我的方式,它们不起作用
这是我的JSON字符串
{ "LocalReferenceNumber":"DNXLHR1906000000000005","DeclarationStatus":"Submitted","Payload":"{\"DeclarationType\":null,\"AcceptanceDateUtc\":null,\"DeclarationUcr\":\"9GB949032610000-AI-000000031-ASH\",\"LocalReferenceNumber\":\"DNXLHR1906000000000005\",\“交易参考\”:\“AI-000000031-ASH\”,\“导出器\”:{\“标识号\”:null,\“名称\”:null,\“街道\”:null,\“城市\”:null,\“邮政编码”:null,\“国家\”:null},\“导入器\”:{\“IdentificationNumber\”:null,\“名称\”:null,\“街道\”:null,\“城市\”:null,\“邮政编码\”:null,\“国家\”:null},\“卖家\”:{\“IdentificationNumber\”:null,\“名称\”:null,\“街道\”:null,\“城市\”:null,\“邮政编码”:null,\“国家\”:null},\“买方\”:{\“标识号\”:null,\“名称\”:null,\“街道\”:null,\“城市\”:null,\“邮政编码\”:null,\“国家\”:null},\"Declarant\":{\"IdentificationNumber\":\"GB949032610000\",\“名称\”:\“ASM (UK)有限公司”,\“街道\”:\“阿什福德住宅\”,\“城市\”:\“阿什福德\”,\“邮政编码\”:\“TW15 2TQ\",\”国家\“:\”GB\“},\"Representative\":{\"IdentificationNumber\":null,\”名称\“:空,\”街道\“:空,\”城市\“:空,\”邮政编码\“:空,\”国家\“:空},\”表示法\“: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,\“NatureOfTransaction\”\"GoodsLocation\":{\"CountryCode\":null,\"Type\":null,\“量词\”:null,\"Quantifier\":null,\"Name\":null},\"FirstDeferment\":null,\"SecondDeferment\":null,\"WarehouseIdentity\":null,\"WarehouseType\":null,\"SupervisingOffice\":null,\"AirportOfLoading\":null,\"MovementReferenceNumber\":null,\"AuthorisationHolders\":[],\“容器\”:[],\“保证\”:[],\“附加财务引用\”:[],\“附加供应链执行元\”:[],\“前一个文档\”:{\“类别\”:\“Z\”,\“类型\”:\“DCR\”,\"Reference\":\"9GB949032610000-AI-000000031-ASH\",\“标识符\”:空,\“顺序\”:1},\“项目\”:[{\“项目编号\”:1,\"DeclarationUcr\":null,\"TraderReference\":null,\"CountryOfOrigin\":null,\"PreferentialCountryOfOrigin\":null,\“Preference PreferentialCountryOfOrigin\”:null,\“Preference GrossMass\”:null,\"Quota\":null,\"GoodsDescription\":\"DSAFASDFSA\",\"ItemPrice\":null,\"ItemCurrency\":null,\"StatisticalValue\":null,\“StatisticalValue ItemCurrency\”:null,\"NetMass\":null,\"GrossMass\":null,\"SupplementaryUnits\":null,\“SupplementaryUnits\”\"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,\“街道\”:null,\“城市\”:null,\“邮政编码”:null,\“国家\”:null},\“买方\”:{\“IdentificationNumber\”:null,\“名称\”:null,\“街道\”:null,\“城市\”:null,\“邮政编码\”:null,\“国家\”:null},\"CustomsUnionAndStatisticsNumber\":null,\“商品编码\”:null,\"ProcedureCode\":null,\“商品附加代码\”:[],\“附加过程代码\”:[],\“以前的文档\”:[],\“容器\”:[],\“程序包\”:[],\“FiscalReference\”:[],\“SupplyChainActor\”:[],\“附加和扣除\”:[],\“附加信息\”:[],\"DocumentCertificateAuthorisationReferences\":[],\“收费行\”:[]}],\“其他附加和派生\”:[]}“"SubmittingBy":"devbuilder","SubmittedOn":"2019-06-14T09:08:42.788Z“}
正如你所看到的,在'JSON wrapper‘中有一个包含JSON数据的'Payload’部分,我需要格式化整个字符串,使其看起来像格式化的JSON数据,即适当的缩进等。
发布于 2019-08-09 17:36:02
最终从字符串中剥离了有效负载:
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; }
}最后,我只对包装器数据进行了格式化:
本地参考号: DNXLHR1906000000000005
申报状态:已提交
提交者: devbuilder
提交日期:"2019-06-14 09:08:42
接下来是我美化的JSON有效负载
这是一种变通方法,但在我的情况下同样有效。
https://stackoverflow.com/questions/57416519
复制相似问题