你可以从JSON在.NET中实例化一个对象实例? [英] Can you Instantiate an Object Instance from JSON in .NET?

查看:659
本文介绍了你可以从JSON在.NET中实例化一个对象实例?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

由于对象初始化非常相似,JSON,现在也有在.NET匿名类型。这将是很酷能够采取一个字符串,如JSON,并创建表示JSON字符串的匿名对象。

Since Object Initializers are very similar to JSON, and now there are Anonymous Types in .NET. It would be cool to be able to take a string, such as JSON, and create an Anonymous Object that represents the JSON string.

使用对象初始化来创建一个匿名类型

Use Object Initializers to create an Anonymous Type:

var person = new {
    FirstName = "Chris",
    LastName = "Johnson"
};



这将是真棒,如果你能在对象初始化代码(最好是类似的字符串表示通过JSON)来创建一个匿名类型的实例与数据。

It would be awesome if you could pass in a string representation of the Object Initializer code (preferably something like JSON) to create an instance of an Anonymous Type with that data.

我不知道这是否是可能的,因为C#是不是动态的,编译器实际转换对象初始化一个ð匿名类型为强类型的代码,可以运行。这在这篇文章解释说。

I don't know if it's possible, since C# isn't dynamic, and the compiler actually converts the Object Initializer and Anonymous Type into strongly typed code that can run. This is explained in this article.

也许功能采取JSON并创建一个键/值字典与它最适合。

Maybe functionality to take JSON and create a key/value Dictionary with it would work best.

我知道你可以序列化器/解串对象到JSON在.NET中,但什么我寻找的是创建本质上是弱类型的对象,类似的JavaScript是如何工作的一种方式。 ?

I know you can serialize/deserializer an object to JSON in .NET, but what I'm look for is a way to create an object that is essentially loosely typed, similarly to how JavaScript works.

有谁知道在.NET中这样做的最佳解决方案。

Does anyone know the best solution for doing this in .NET?

更新:太明确的背景下为什么我问这个......我在想如何C#可以更好地在语言层面(可能)都支持JSON和我试图想它可能今天做到的,观念上的原因方式。所以,我想我会后在这里抛砖引玉。

UPDATE: Too clarify the context of why I'm asking this... I was thinking of how C# could better support JSON at the language level (possibly) and I was trying to think of ways that it could be done today, for conceptual reasons. So, I thought I'd post it here to start a discussion.

推荐答案

有用于.NET语言有duck-打字但因为C#要求所有成员引用在编译时解决它不可能使用Dot.Notation C#。如果你想使用Dot.Notation,你还是要具有所需性能的某处定义一个类,并使用您希望从JSON数据类实例任何方法。预先定义类的确实的有像强类型,IDE支持,包括智能感知,而不是担心拼写错误的好处。您仍然可以使用匿名类型:

There are languages for .NET that have duck-typing but it's not possible with C# using Dot.Notation since C# requires that all member references are resolved at compile time. If you want to use the Dot.Notation, you still have to define a class somewhere with the required properties, and use whatever method you want to instantiate the class from the JSON data. Pre-defining a class does have benefits like strong typing, IDE support including intellisense, and not worrying about spelling mistakes. You can still use anonymous types:

 T deserialize<T>(string jsonStr, T obj) { /* ... */}

 var jsonString = "{FirstName='Chris', LastName='Johnson, Other='unused'}";
 var person     = deserialize(jsonString, new {FirstName="",LastName=""});
 var x          = person.FirstName; //strongly-typed

这篇关于你可以从JSON在.NET中实例化一个对象实例?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆