简体   繁体   English

将 JSON 转换为对象/字典/动态

[英]converting JSON to an object / dictionary / dynamic

I'm currently using jqGrid to display data.我目前正在使用jqGrid来显示数据。 Part of jqGrid's interface will give you search options, posting back the search details in a JSON string, for example: jqGrid 的部分界面将为您提供搜索选项,以 JSON 字符串回发搜索详细信息,例如:

{
  "groupOp": "AND",
  "rules": [{
    "field": "PersonID",
    "op": "eq",
    "data": "123"
  }, {
    "field": "LastName",
    "op": "eq",
    "data": "Smith"
  }]
}

(meaning i'm searching for personID = 123, and LastName = 'Smith') (意思是我正在搜索 personID = 123 和 LastName = 'Smith')

so what i'm hoping to do is somehow convert that back into something i can use server-side.所以我希望做的是以某种方式将它转换我可以在服务器端使用的东西。

Does anyone have a solution for this that may convert it back into an object of some kind?有没有人对此有解决方案可以将其转换回某种对象? My current solution would be to convert into xml, parse with linq and create instances of my own 'search' class with a 'rules' collection.我当前的解决方案是转换为 xml,使用 linq 解析并使用“规则”集合创建我自己的“搜索”类的实例。

Json.NET is a very rich JSON parser for .NET (I know, Captain Obvious huh...) Json.NET是一个非常丰富的 .NET JSON 解析器(我知道,Captain Obvious 嗯...)

As far as I know, using the built-in JsonDataContractSerializer won't do what you're looking for because it requires a known type that provides the data contract.据我所知,使用内置的 JsonDataContractSerializer 不会做你正在寻找的东西,因为它需要一个提供数据契约的已知类型。 Without an existing type to deserialize to, it can't just produce a dynamic object.如果没有要反序列化的现有类型,它就不能只生成动态对象。

您可以使用JavaScriptSerializer在 json 和您的 Search 类之间进行序列化/反序列化。

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

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