我将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'apasété géréeHResult= -2146233088 Message = JSON根对象有多个 属性。根对象必须具有单个属性才能 创建一个有效的XML文档。考虑指定一个 DeserializeRootElementName。路径'DraftNumber',第1行,第57位。
Source = Newtonsoft.Json StackTrace: àNewtonsoft.Json.Converters.XmlNodeConverter.DeserializeNode(JsonReader) reader,IXmlDocument文档,XmlNamespaceManager管理器,IXmlNode currentNode) àNewtonsoft.Json.Converters.XmlNodeConverter.ReadJson(JsonReader reader,Type objectType,Object existingValue,JsonSerializer 串行) àNewtonsoft.Json.Serialization.JsonSerializerInternalReader.DeserializeConvertable(JsonConverter) converter,JsonReader reader,Type objectType,Object existingValue) àNewtonsoft.Json.Serialization.JsonSerializerInternalReader.Deserialize(JsonReader) reader,Type objectType,Boolean checkAdditionalContent) àNewtonsoft.Json.JsonSerializer.DeserializeInternal(JsonReader reader,Type objectType) àNewtonsoft.Json.JsonConvert.DeserializeObject(字符串值,类型类型,JsonSerializerSettings设置) àNewtonsoft.Json.JsonConvert.DeserializeObject(字符串值,类型类型,JsonConverter []转换器) àNewtonsoft.Json.JsonConvert.DeserializeXmlNode(String value,String deserializeRootElementName,Boolean writeArrayAttribute) àNewtonsoft.Json.JsonConvert.DeserializeXmlNode(String value) àConsoleApplication3.Program.Main(String [] args)dans c:\ documents \ s638723 \ documents \ visual studio 2015 \ Projects \ ConsoleApplication3 \ Program.cs:ligne 17 àSystem.AppDomain._nExecuteAssembly(RuntimeAssembly assembly,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回调,对象状态,布尔值 preserveSyncCtx) àSystem.Threading.ExecutionContext.Run(ExecutionContext executionContext,ContextCallback回调,对象状态,布尔值 preserveSyncCtx) àSystem.Threading.ExecutionContext.Run(ExecutionContext executionContext,ContextCallback回调,对象状态) àSystem.Threading.ThreadHelper.ThreadStart()InnerException:
更全局我想知道mongoexport
导出的类型是否可供jsonconverter使用。
答案 0 :(得分:0)
正如异常所述,JSON在顶层有multiple
个属性。
只需定义root
,如:
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>