简体   繁体   English

将 Json 解析为模式生成的一组类

[英]Parse Json into a schema-generated set of classes

I have a Json schema and can use NJsonSchema.CodeGeneration.CSharp to create classes corresponding to it.我有一个 Json 模式,可以使用NJsonSchema.CodeGeneration.CSharp创建NJsonSchema.CodeGeneration.CSharp对应的类。 So if I have json which conforms to the schema, I should be able to easily parse it into a collection of objects whose classes are the classes generated from the schema?因此,如果我有符合模式的 json,我应该能够轻松地将其解析为一个对象集合,这些对象的类是从模式生成的类?

So how can I do this - parse json and get out C# class objects that correspond to the objects in the json (as define by the schema)?那么我该怎么做 - 解析 json 并取出与 json 中的对象(由架构定义)对应的 C# 类对象?

As an example - if the schema defines a first object definition which is an array of a second object definition, then I would like to be able to parse it in such a way that the output is an instance of the class corresponding to the first object definition and it has a member which is a List of instances of the class corresponding to the second definition.例如 - 如果模式定义了第一个对象定义,它是第二个对象定义的数组,那么我希望能够以这样的方式解析它,即输出是对应于第一个对象的类的实例定义并且它有一个成员,它是与第二个定义对应的类的实例List It seems that the schema knows all the information required to do this, so it should be a single line - I appreciate I could do long-hand parsing (eg converting each item in the array) to achieve the same result.模式似乎知道执行此操作所需的所有信息,因此它应该是一行 - 我很感激我可以进行长时间解析(例如转换数组中的每个项目)以实现相同的结果。

I would think this would be a primary purpose of having C# classes generated from a schema, so what's the magic method I'm missing?我认为这将是从模式生成 C# 类的主要目的,那么我缺少的魔法方法是什么?

I'm also happy to write C# classes and generate a schema from that if it's a more workable solution.如果它是一个更可行的解决方案,我也很乐意编写 C# 类并从中生成一个架构。

I've used NJsonSchema but happy to use any other C# json schema and code generation technique that achieves the same end.我使用过NJsonSchema但很高兴使用任何其他 C# json 模式和代码生成技术来实现相同的目的。

UPDATE: After discussion I've seen that if NJsonSchema is used to generate classes from the schema, the TypeScript version of those classes each have a fromJS method which sounds like what I want but they're missing from the C# version.更新:经过讨论,我看到如果NJsonSchema用于从模式生成类,这些类的TypeScript版本每个都有一个fromJS方法,这听起来像我想要的,但它们在 C# 版本中丢失了。 I'm happy to use something other than NJsonSchema to generate classes from schema if it provides a solution.如果它提供了解决方案,我很高兴使用NJsonSchema以外的其他东西从架构生成类。

I think I found the answer, which was a lot simpler than I had anticipated.我想我找到了答案,这比我预期的要简单得多。 It's simply to use something like:它只是使用类似的东西:

var ob=JsonConvert.DeserializeObject<MyNamespace.Anonymous>(jsonString);

...where MyNamespace is the namespace of the generated C# classes, MyNamespace.Anonymous is the class corresponding to the root of the schema (default names from NJsonSchema ), and jsonString the string to be parsed. ...其中MyNamespace是生成的 C# 类的命名空间, MyNamespace.Anonymous是与模式根对应的类(来自NJsonSchema默认名称), jsonString是要解析的字符串。

I thought the solution would need to be schema-aware, since it would need to know about all the classes created from the schema, but I guess it 'works that out' from reflecting on the Anonymous class it's given and where the properties of it are classes, reflecting on those and so on.我认为该解决方案需要具有模式感知能力,因为它需要了解从模式创建的所有类,但我想它可以通过反映它给出的Anonymous类以及它的属性来“解决”是类,反思那些等等。

I was over-thinking the problem!是我多虑了问题!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM