简体   繁体   English

如何在不知道字段名称的情况下反序列化json字符串?

[英]How can I deserialize a json string without knowing the name of the fields?

I have a string that can look like this: 我有一个字符串如下所示:

[  
   {  
      Id:{  
         editable:false,
         nullable:true
      }
   },
   {  
      Product:{  
         validation:{  
            required:true
         }
      }
   },
   {  
      Cost:{  
         type:"number",
         validation:{  
            required:true
         }
      }
   }
]

It can also look like this: 它也可能像这样:

[  
   {  
      Id:{  
         editable:false,
         nullable:true
      }
   },
   {  
      Car:{  
         validation:{  
            required:true
         }
      }
   },
   {  
      Make:{  
         validation:{  
            required:true
         }
      }
   }
]

The point is that the string will always have an Id field with some predictable values, but additional fields can be named anything. 关键是该字符串将始终具有一个带有一些可预测值的Id字段,但其他字段可以命名为任何东西。

I have some javascript which I need to look like this: 我有一些JavaScript,我需要看起来像这样:

schema: {
   model: {
      id: "Id",
      fields: {
          Id: { editable: false, nullable: true },
          Product: { validation: { required: true } },
          Cost: { type: "number", validation: { required: true }}
      }
   }
}

... where the content of fields needs to correlate the string. ...字段的内容需要使字符串相关联。

Currently I have an ajax call that get some other data my code needs and I would like to also give it the results I need for the fields. 当前,我有一个ajax调用,它获取代码所需的其他一些数据,并且我还希望为它提供字段所需的结果。 I would like to replace the json like so: 我想像这样替换json:

schema: {
   model: {
      id: "Id",
      fields: ajaxResult.fields
      }
   }
}

The normal route is to create a class (or classes) to which I can deserialize the json string, but in this case the fields in the json string can be completely arbitrary. 正常的方法是创建一个或多个我可以反序列化json字符串的类,但是在这种情况下,json字符串中的字段可以完全是任意的。 So I cannot create classes for this since I don't know what the properties will be called . 所以我无法为此创建类,因为我不知道该调用什么属性

How can I deserialize this string so when I return it from my Action it will work as I describe? 我如何反序列化此字符串,以便当我从Action中返回它时,它将按我描述的那样工作?

Currently my controller action looks something like this: 目前,我的控制器动作如下所示:

public IActionResult GetJsonData(Guid id)
{
   var model = new GridDataModel
   {
       schema = SchemaToJson(id),
       //fields = FieldsToJson(id),
       gridData = RowsToJson(id)
   };
   return Json(model);
}

In Visual Studio 2015, you can use the "paste special" function, Visual studio will generate all classes (including nested classes etc.) for you, no single line of your own coding is needed: 在Visual Studio 2015中,您可以使用“特殊粘贴”功能,Visual Studio将为您生成所有类(包括嵌套类等),不需要您自己的编码的任何一行:

在此处输入图片说明

See animations below: (original GIF is from here .) 请参见下面的动画:(原始GIF来自此处 。)

在此处输入图片说明

在此处输入图片说明

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

相关问题 Javascript:如何在不知道键名的情况下解析json数组? - Javascript: How to parse a json array without knowing the key name? 如何在不知道其结构和数据的情况下以可读的方式显示HTML中的JSON信息? - How can I show information from a JSON in HTML in a readable manner without knowing its structure and data? 如何在不知道每个字符串的大小的情况下从数组中的每个字符串中获取子字符串? - How can I get a substring from each string in an array, without knowing the size of each? 在不知道名称的情况下检索json元素 - Retrieve a json element without knowing the name json在不知道对象名称的情况下获取对象 - json get object without knowing it's name 如何在for循环中访问json数组的字段名称? - How can I access the name of the fields of a json array inside a for loop? 如何在不知道值名的情况下从 JSON 文件中获取单个值和值名 - How to get a single value and value name from an JSON file without knowing the value name 在不知道请求的情况下如何获得Ajax响应? - How can I get a Ajax-Response without knowing the Request? 如何在不知道宽度的情况下居中对齐div? - How can I center align a div without knowing the width? 如何使用 JavaScript 读取没有父根名称的 JSON? - How can I read a JSON without parent root name with JavaScript?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM