无法将大型Javascript对象转换为C#对象

时间:2022-11-30 22:24:53

I'm coming from a node.js javascript everywhere upbringing. Learning .NET and struggling mightily with the strongly typed aspect of it. What is the quickest way to convert a large JSON object:

我来自一个node.js javascript无处不在的教养。学习.NET并与其强烈类型的方面进行艰苦的斗争。转换大型JSON对象的最快方法是什么:

var body = new {
                   "stay": {
                       "checkIn": "2016-06-08",
                       "checkOut": "2016-06-10",
                       "shiftDays": "2"
                   },
                   "occupancies": [
                                      {
                                          "rooms": 1,
                                          "adults": 2,
                                          "children": 1,
                                          "paxes": [
                                                       {
                                                           "type": "AD",
                                                           "age": 30
                                                       },
                                                       {
                                                           "type": "AD",
                                                           "age": 30
                                                       },
                                                       {
                                                           "type": "CH",
                                                           "age": 8
                                                       }
                                                   ]
                                       }
                                   ],
                   "geolocation": {
                                      "longitude": 2.646633999999949,
                                      "latitude": 39.57119,
                                      "radius": 20,
                                      "unit": "km"
                                  }
               };

Into something that can be read in Visual Studio?

进入可以在Visual Studio中读取的东西?

3 个解决方案

#1


4  

Acutally there are some possibilities but I'll point out 3 for you:

实际上有一些可能性,但我会为你指出3:

  1. You can create the same class hierarchy as your json object represents and deserialize your json into an instance of this created class.
  2. 您可以创建与json对象相同的类层次结构,并将json反序列化为此创建的类的实例。
  3. You can Visual Studio take care of creating these classes by copying your json and using Edit > Paste Special > Paste JSON as Classes.
  4. Visual Studio可以通过复制json并使用“编辑”>“选择性粘贴”>“将JSON粘贴为类”来创建这些类。
  5. You can use the datatype dynamic and just deserialize into this datatype. The code would look like this: dynamic json = JsonConvert.DeserializeObject(yourJsonAsText);. If you do so you can programatically access everything within the deserialized instance but...
    • you will not have intellisense (because there is no class existing)
    • 你不会有intellisense(因为没有类存在)
    • could maybe access something that "isnt there"
    • 也许可以访问“不存在”的东西
  6. 您可以使用动态数据类型,只需反序列化为此数据类型。代码如下所示:dynamic json = JsonConvert.DeserializeObject(yourJsonAsText);.如果你这样做,你可以以编程方式访问反序列化实例中的所有内容,但是...你将没有intellisense(因为没有类存在)可能会访问“不存在”的内容

However you will need to install the Netwonsoft.Json package for the above 3 solutions. If you need a way without a 3rd party component/package, you can take a look at the following answers:

但是,您需要为上述3种解决方案安装Netwonsoft.Json软件包。如果您需要一种没有第三方组件/包的方法,您可以查看以下答案:

They show some ways provided by the .NET framework itself.

它们展示了.NET框架本身提供的一些方法。

#2


0  

Install https://www.nuget.org/packages/Newtonsoft.Json/ if you are not using it yet then

如果你还没有使用它,请安装https://www.nuget.org/packages/Newtonsoft.Json/

JsonConvert.DeserializeObject<YourType>(string)

Other option, if you use asp.net, you can do is to create WebApi or MVc action and input parameter would be type you Send and .net will deserialize it for you

其他选项,如果你使用asp.net,你可以做的是创建WebApi或MVc动作,输入参数将是你发送的类型,.net将为你反序列化它

public void ActionName(YourType type){
}

#3


0  

You could try a library that will convert data for you like Jayrock or Json.NET

您可以尝试使用Jayrock或Json.NET为您转换数据的库

https://atifaziz.github.io/projects/jayrock/ or http://www.newtonsoft.com/json

https://atifaziz.github.io/projects/jayrock/或http://www.newtonsoft.com/json

These will convert your object into an object that doesn't have a specific type.

这些将您的对象转换为没有特定类型的对象。

#1


4  

Acutally there are some possibilities but I'll point out 3 for you:

实际上有一些可能性,但我会为你指出3:

  1. You can create the same class hierarchy as your json object represents and deserialize your json into an instance of this created class.
  2. 您可以创建与json对象相同的类层次结构,并将json反序列化为此创建的类的实例。
  3. You can Visual Studio take care of creating these classes by copying your json and using Edit > Paste Special > Paste JSON as Classes.
  4. Visual Studio可以通过复制json并使用“编辑”>“选择性粘贴”>“将JSON粘贴为类”来创建这些类。
  5. You can use the datatype dynamic and just deserialize into this datatype. The code would look like this: dynamic json = JsonConvert.DeserializeObject(yourJsonAsText);. If you do so you can programatically access everything within the deserialized instance but...
    • you will not have intellisense (because there is no class existing)
    • 你不会有intellisense(因为没有类存在)
    • could maybe access something that "isnt there"
    • 也许可以访问“不存在”的东西
  6. 您可以使用动态数据类型,只需反序列化为此数据类型。代码如下所示:dynamic json = JsonConvert.DeserializeObject(yourJsonAsText);.如果你这样做,你可以以编程方式访问反序列化实例中的所有内容,但是...你将没有intellisense(因为没有类存在)可能会访问“不存在”的内容

However you will need to install the Netwonsoft.Json package for the above 3 solutions. If you need a way without a 3rd party component/package, you can take a look at the following answers:

但是,您需要为上述3种解决方案安装Netwonsoft.Json软件包。如果您需要一种没有第三方组件/包的方法,您可以查看以下答案:

They show some ways provided by the .NET framework itself.

它们展示了.NET框架本身提供的一些方法。

#2


0  

Install https://www.nuget.org/packages/Newtonsoft.Json/ if you are not using it yet then

如果你还没有使用它,请安装https://www.nuget.org/packages/Newtonsoft.Json/

JsonConvert.DeserializeObject<YourType>(string)

Other option, if you use asp.net, you can do is to create WebApi or MVc action and input parameter would be type you Send and .net will deserialize it for you

其他选项,如果你使用asp.net,你可以做的是创建WebApi或MVc动作,输入参数将是你发送的类型,.net将为你反序列化它

public void ActionName(YourType type){
}

#3


0  

You could try a library that will convert data for you like Jayrock or Json.NET

您可以尝试使用Jayrock或Json.NET为您转换数据的库

https://atifaziz.github.io/projects/jayrock/ or http://www.newtonsoft.com/json

https://atifaziz.github.io/projects/jayrock/或http://www.newtonsoft.com/json

These will convert your object into an object that doesn't have a specific type.

这些将您的对象转换为没有特定类型的对象。