如何使用JSON.net引用外部文件?这是我在JSON.net尝试读取我的JSON架构时遇到的错误(returnJsonSchema.Read(newJsonTextReader(reader));):2014-07-1515:33:42.7011[致命]Newtonsoft.Json.JsonException:无法解析架构引用“data-result.json”。atNewtonsoft.Json.Schema.JsonSchemaBuilder.ResolveReferences(JsonSchemaschema)inc:DevelopmentReleasesJsonWorkingNewtonsoft.JsonSrcNewtonsoft.JsonSchemaJsonSchemaBuilder.cs:line139at.JsonSchemaBuilder.ResolveReferences(JsonSchemaschema)inc:DevelopmentReleasesJsonWorkingNewtonsoft.JsonSrcNewtonsoft.JsonSchemaJsonSchemaBuilder.cs:line179在Newtonsoft.Json.Schema.JsonSchemaBuilder.Read(JsonReader阅读器)在c:DevelopmentReleasesJsonWorkingNewtonsoft.JsonSrcNewtonsoft.JsonSchemaJsonSchemaBuilder.cs:第85行在Newtonsoft.Json.Schema.JsonSchema.Read(JsonReader阅读器,JsonSchemaResolver解析器)在c:DevelopmentReleasesJsonsoftWorking.JsonSrcNewtonsoft.JsonSchemaJsonSchema.cs:C中Newtonsoft.Json.Schema.JsonSchema.Read(JsonReader阅读器)的第280行:DevelopmentReleasesJsonWorkingNewtonsoft.JsonSrcNewtonsoft.JsonSchemaJsonSchema.cs:ThinkBinary.SchemaToPoco.Core.JsonSchemaToCodeUnit.LoadSchema(字符串文件)中的第266行:UsersSLiuProjectsjson-schema-to-pocoSourceThinkBinary.SchemaToPoco.CoreJsonSchemaToCodeUnit.cs:第70行在ThinkBinary.SchemaToPoco.Core.JsonSchemaToCodeUnit..ctor(StringschemaDocument,StringrequestedNamespace)在c:UsersSLiuProjectsjson-schema-to-pocoSourceThinkBinary.SchemaToPoco.SchemaToPocoCoreJsonSchemaToCodeUnit.cs:line19atThinkBinary.SchemaToPoco.Console.Program.Main(String[]args)inc:UsersSLiuProjectsjson-schema-to-pocoSourceThinkBinary.SchemaToPoco.ConsoleProgram.cs:line38我的JSON结构:{"$schema":"http://json-schema.org/draft-03/schema#","title":"DataSet","description":"结果集和度量值的描述","type":“对象”,“属性”:{“结果”:{"$ref":"data-result.json"},"dimensions":{"type":"array","description":"结果集中包含的数据维度数组","items":{"$ref":"data-dimension.json"},"uniqueItems":true},"measure":{"$ref":"data-measure.json","description":"此数据中表示的单个度量放。”我的问题是我有这个引用外部文件data-result.jsonresult.json的JSON模式,但JSON.net还不知道它存在任何解决方法?我的一个想法是浏览模式,如果有任何对外部文件的引用,则使用公共JsonSchemaResolver解决这些问题。我必须在适当的地方添加ID,因为$ref似乎喜欢按ID匹配,即使在json-schema.org上也有明确的$ref与文件名一起使用的示例。我想知道JSON.net是否有更好的方法来支持引用外部模式。如果有帮助,源代码托管在Github上。我已经测试删除$ref字段并成功编译。Json.NETSchema对解析外部引用的支持有了很大改进。在此处阅读更多信息:http://www.newtonsoft.com/jsonschema/help/html/LoadingSchemas.htm我的一个想法是浏览架构,如果有任何对外部文件的引用,请使用通用的JsonSchemaResolver解决这些文件,您需要知道您的模式依赖于哪些模式,首先解析它们并将它们添加到JsonSchemaResolver。架构将使用其ID进行解析。这是一个示例(使用draft-03语法):varbaseSchema=JsonSchema.Parse(@"{""$schema"":""http://json-schema.org/draft-03/schema#"",""id"":""http://mycompany/base-schema#",""type"":""object",""properties"":{""firstName"":{""type"":""string"",""required"":true}}}");varresolver=newJsonSchemaResolver{LoadedSchemas={baseSchema}};varderivedSchema=JsonSchema。解析(@"{""$schema"":""http://json-schema.org/draft-03/schema#"",""id"":""http://mycompany/derived-schema#"",""type"":""object"",""extends"":{""$ref"":""http://mycompany/base-schema#""},""properties"":{""lastName"":{""type"":""string"",""required"":true}}}",resolver);Fiddle:https://dotnetfiddle.net/g1nFew我认为问题可能是你在那些$ref项中有相对URI,但没有id属性来建立基本URI。您可以通过将id=""添加到最外层的上下文来解决您的问题,从而确定可以从何处检索这些外部文件。请参阅http://json-schema.org/latest/json-schema-core.html的第7节PS在您的度量项目中,我注意到您有$ref和description值。json-referenceinternet-draft声明除了“$ref”之外的任何JSON引用对象成员都应该被忽略。它可能没有任何危害,但如果有人希望该值覆盖外部文件中的描述,则可能会令人惊讶。以上就是C#学习教程:如何使用JSON.net引用外部文件?如果所有分享的内容对你有用,需要进一步了解C#学习教程,希望大家多多关注。本文收集自网络,不代表立场。如涉及侵权,请点击右侧联系管理员删除。如需转载请注明出处:
