【问题标题】:Converting mongoexport json to XML将 mongoexport json 转换为 XML
【发布时间】:2018-08-15 19:04:15
【问题描述】:

我将 mongoDb 数据库中的数据导出到 json 文件中,并且需要将其解析为 XML 以便在我们的 ETL 中使用。

这是我尝试用来解析我的 json 导出的一行的代码示例:

var json = @"{""_id"":{""$oid"":""592bbd86b029e62830c5020a""},""DraftNumber"":""A1B1CB8D"",""ProductRange"":""COMPREHENSIVE_HOME_INSURANCE"",""DraftStatus"":""QUOTATION_DRAFT"",""DraftLabel"":"""",""LastUpdateDate"":{""$date"":""2017-05-29T06:19:53.559Z""},""EndDate"":{""$date"":""2017-07-28T06:19:53.559Z""},""UserId"":""D900036"",""ProjectNumber"":""38764496"",""Identifier"":"""",""CurrencyCode"":""EUR"",""RenewalDate"":{""$date"":""0001-01-01T00:00:00.000Z""},""RenewalDay"":0,""RenewalMounth"":0,""CreationDate"":{""$date"":""2017-05-29T06:19:50.138Z""},""EffectiveHour"":""0"",""EffectiveMinute"":""0"",""HasAs"":[{""_t"":""AgreementHolder"",""_id"":{""$oid"":""000000000000000000000000""},""DistribCustomer"":{""isProspect"":true}}],""IsBasedOnProduct"":{""pricingType"":""DISCOUNT_RATE"",""pricingVersion"":""C""},""ActivityInAgreements"":[{""_t"":""AgreementRequest"",""_id"":{""$oid"":""000000000000000000000000""},""PremiumNature"":null}],""OriginalSubscriptionChannel"":""DIRECT"",""CurrentSubscriptionChannel"":""DIRECT"",""BusinessExpirationDate"":{""$date"":""0001-01-01T00:00:00.000Z""},""TechnicalExpirationDate"":{""$date"":""2017-08-27T06:19:53.559Z""},""IsEligibleToProposal"":true,""IneligibityReasonCodes"":[],""DematerialisationOfDocuments"":true}";

var doc = (XmlDocument)JsonConvert.DeserializeXmlNode(json);

Console.WriteLine(doc);
Console.ReadKey();

我面临以下异常:

L'exception Newtonsoft.Json.JsonSerializationException n'a pas été gérée HResult=-2146233088 Message=JSON 根对象有多个 特性。根对象必须具有单个属性才能 创建一个有效的 XML 文档。考虑指定一个 反序列化根元素名称。路径“DraftNumber”,第 1 行,位置 57。
来源=Newtonsoft.Json StackTrace: à Newtonsoft.Json.Converters.XmlNodeConverter.DeserializeNode(JsonReader 阅读器、IXmlDocument 文档、XmlNamespaceManager 管理器、IXmlNode 当前节点) à Newtonsoft.Json.Converters.XmlNodeConverter.ReadJson(JsonReader reader, 类型 objectType, Object existingValue, JsonSerializer 序列化器) à Newtonsoft.Json.Serialization.JsonSerializerInternalReader.DeserializeConvertable(JsonConverter 转换器、JsonReader 阅读器、类型 objectType、Object existingValue) à Newtonsoft.Json.Serialization.JsonSerializerInternalReader.Deserialize(JsonReader 阅读器,类型 objectType,布尔检查附加内容) à Newtonsoft.Json.JsonSerializer.DeserializeInternal(JsonReader reader, Type objectType) à Newtonsoft.Json.JsonConvert.DeserializeObject(String value, Type type, JsonSerializerSettings设置) à Newtonsoft.Json.JsonConvert.DeserializeObject(String value, Type type, JsonConverter[] 转换器) à Newtonsoft.Json.JsonConvert.DeserializeXmlNode(String value, String deserializeRootElementName, Boolean writeArrayAttribute) à Newtonsoft.Json.JsonConvert.DeserializeXmlNode(字符串值) à ConsoleApplication3.Program.Main(String[] args) dans c:\documents\s638723\documents\visual studio 2015\Projects\ConsoleApplication3\Program.cs:ligne 17 à System.AppDomain._nExecuteAssembly(RuntimeAssembly 程序集,String[] args) à System.AppDomain.ExecuteAssembly(String assemblyFile, Evidence assemblySecurity, String[] args) à Microsoft.VisualStudio.HostingProcess.HostProc.RunUsersAssembly() à System.Threading.ThreadHelper.ThreadStart_Context(对象状态) à System.Threading.ExecutionContext.RunInternal(ExecutionContext executionContext、ContextCallback 回调、对象状态、布尔值 保留SyncCtx) à System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state, Boolean 保留SyncCtx) à System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state) à System.Threading.ThreadHelper.ThreadStart() InnerException:

在全球范围内,我想知道 mongoexport 导出的类型是否可以被 jsonconverter 使用。

【问题讨论】:

  • @jdweng 你确定吗? newtonsoft.com/json/help/html/…
  • 格式良好的 Xml 在根级别有一个标签。您的 JSON 在根级别有一个数组。所以转换给出了一个错误,因为生成的 xml 在根中有多个元素。要使代码正常工作,您需要用 root 包装 Json:{“Root”:{“_id”:{.........}}

标签: c# json xml mongodb json.net


【解决方案1】:

如例外所述,JSON 在顶层具有 multiple 属性。

简单定义rootlike:

using (StreamReader r = new StreamReader("Json_1.json"))
{
    string json = r.ReadToEnd();
    var doc = (XmlDocument)JsonConvert.DeserializeXmlNode(json, "root");
}

输出:

<root>
    <_id>
        <_x0024_oid>592bbd86b029e62830c5020a</_x0024_oid>
    </_id>
    <DraftNumber>A1B1CB8D</DraftNumber>
    <ProductRange>COMPREHENSIVE_HOME_INSURANCE</ProductRange>
    <DraftStatus>QUOTATION_DRAFT</DraftStatus>
    <DraftLabel/>
    <LastUpdateDate>
        <_x0024_date>2017-05-29T06:19:53.559Z</_x0024_date>
    </LastUpdateDate>
    <EndDate>
        <_x0024_date>2017-07-28T06:19:53.559Z</_x0024_date>
    </EndDate>
    <UserId>D900036</UserId>
    <ProjectNumber>38764496</ProjectNumber>
    <Identifier/>
    <CurrencyCode>EUR</CurrencyCode>
    <RenewalDate>
        <_x0024_date>0001-01-01T00:00:00Z</_x0024_date>
    </RenewalDate>
    <RenewalDay>0</RenewalDay>
    <RenewalMounth>0</RenewalMounth>
    <CreationDate>
        <_x0024_date>2017-05-29T06:19:50.138Z</_x0024_date>
    </CreationDate>
    <EffectiveHour>0</EffectiveHour>
    <EffectiveMinute>0</EffectiveMinute>
    <HasAs>
        <_t>AgreementHolder</_t>
        <_id>
            <_x0024_oid>000000000000000000000000</_x0024_oid>
        </_id>
        <DistribCustomer>
            <isProspect>true</isProspect>
        </DistribCustomer>
    </HasAs>
    <IsBasedOnProduct>
        <pricingType>DISCOUNT_RATE</pricingType>
        <pricingVersion>C</pricingVersion>
    </IsBasedOnProduct>
    <ActivityInAgreements>
        <_t>AgreementRequest</_t>
        <_id>
            <_x0024_oid>000000000000000000000000</_x0024_oid>
        </_id>
        <PremiumNature />
    </ActivityInAgreements>
    <OriginalSubscriptionChannel>DIRECT</OriginalSubscriptionChannel>
    <CurrentSubscriptionChannel>DIRECT</CurrentSubscriptionChannel>
    <BusinessExpirationDate>
        <_x0024_date>0001-01-01T00:00:00Z</_x0024_date>
    </BusinessExpirationDate>
    <TechnicalExpirationDate>
        <_x0024_date>2017-08-27T06:19:53.559Z</_x0024_date>
    </TechnicalExpirationDate>
    <IsEligibleToProposal>true</IsEligibleToProposal>
    <DematerialisationOfDocuments>true</DematerialisationOfDocuments>
</root>

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2013-05-09
    • 1970-01-01
    • 2015-05-30
    • 2017-08-07
    • 2014-07-30
    • 2017-07-04
    相关资源
    最近更新 更多