简体   繁体   English

是否可以将带有@attributes的JSON序列化为C#类

[英]Is it possible to serialize JSON with @attributes into a C# class

Given a simple snippet of JSON 给定一个简单的JSON代码段

{
    "@attributes" : { 
         "version" : "1.0"
     },
     "item:" : {
         "@attributes" : {
             "name" : "foo",
             "id" : "bar"
          }
     }
}

Is it possible to serialize this into a C# class? 是否可以将其序列化为C#类? Using json2charp.com generates funky invalid class definitions. 使用json2charp.com会生成时髦的无效类定义。

You can do that, provided you have a class definition in place which looks exactly like this json object. 您可以做到这一点,只要您有一个完全类似于此json对象的类定义即可。

`class ABC{  
      Attribute attirbute;
      Item item;
}`

But you'll have to either the change the way in which you write your json or make additional classes. 但是您必须要么更改编写json的方式,要么创建其他类。 Wat i mean is '@attribute' over here points another key-vlaue pair and not a single value. 我的意思是这里的“ @attribute”指向另一个键-vlaue对,而不是单个值。 So, you should have a class which takes version as a member in this case i am writting it as Attribute and than you'll have to put that class a member of the class ABC . 因此,在这种情况下,您应该有一个将version作为成员的类,我将其写为Attribute然后必须将该类作为class ABC类的成员。

检查一下,您可能会发现它有用-我知道我有json2csharp

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

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