如何将对象类型转换为C#类对象类型 [英] How To Convert object type to C# class object type

查看:77
本文介绍了如何将对象类型转换为C#类对象类型的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是一个带有rEST API的示例项目..在我的POST方法中,我传递了一个带有url的stringify对象。我的动作参数是object。我想把这个对象数据转换成我班级的另一个对象..我的示例代码如下



newuser是对象



Json

I'm diong a sample project with rEST API.. In my POST method i pass an stringify object with the url. my parameter for the action is object. Ineed to convert this object data to another object of my class .. my Sample code is below

newuser is object

Json

$.ajax({
    type: "POST",
    url: "values/InsertUserDetails",
    dataType: "json",
    data: JSON.stringify(newuser),
    contentType: "application/json; charset=utf-8",
     
    error: function (error) {
        alert("TEST !!!");
    }
});





C#代码







C# code


public int InsertUserDetails([FromBody]object datatosave)
       {
                
                MdlUser objuser = (MdlUser) (datatosave);
                return SettingManager.InsertUserDetails(objuser);
         } 

推荐答案

.ajax({
type:POST,
url: values / InsertUserDetails,
dataType:json,
data:JSON.stringify(newuser),
contentType:application / json; charset = utf-8,

错误:功能(错误){
alert(TEST !!!);
}
});
.ajax({ type: "POST", url: "values/InsertUserDetails", dataType: "json", data: JSON.stringify(newuser), contentType: "application/json; charset=utf-8", error: function (error) { alert("TEST !!!"); } });





C#代码







C# code


public int InsertUserDetails([FromBody]object datatosave)
       {
                
                MdlUser objuser = (MdlUser) (datatosave);
                return SettingManager.InsertUserDetails(objuser);
         } 


关于转换的问题没有文学意义。 Javascript对象是一回事,.NET对象是另一回事;他们从不一起吃饭;一个是客户端,另一个是服务器端。 (假设你使用的是ASP.NET,因为 - 还有什么?)此外,你没有传递一个stringify对象。



然而,问题,在其中本质,有意义;它是关于.NET中JSON的反序列化。



您在HTTP请求中发送一个sting,这是 newuser的字符串表示使用Ajax(Javascript)语法。

当服务器端代码收到请求时,您需要反序列化此JSON字符串。您可以使用 System.Web.Script.Serialization.JavaScriptSerializer

http://msdn.microsoft.com/en-us/library/system.web.script.serialization。 javascriptserializer%28v = vs.110%29.aspx [ ^ ]。



每种方法 JavaScriptSerializer 尝试将JSON字符串反序列化为.NET对象。有关更多详细信息,请参阅代码示例。另见此示例:

http://dailydotnettips.com/2013/09/26/sending-raw-json-request-to-asp-net-from-jquery [ ^ ]。

此页面上的关键短语是:要解析json请求,您需要自己解析它。然而,事实并非如此。您可以合理地猜测.NET对象的预期类型;然后使用此序列化程序进行反序列化将成功(请参阅我的代码示例演示它,第二个)。



进一步的细节取决于<$ c的类型$ c> newuser 以及你想在服务器端做什么。让我们考虑一个简单的例子:

The question about "Convert" makes no literary sense. Javascript object is one thing, .NET object is another; they never meed together; one is one the client side, another is on the server side. (Assuming you are using ASP.NET, because — what else?) Moreover, you do not "pass an stringify object".

However, the question, in it essence, makes sense; it is about deserialization of JSON in .NET.

You send a sting in your HTTP request, which is a string representation of newuser in Ajax (Javascript) syntax.
When your server-side code receives the request, you will need to deserialize this JSON string. You can use System.Web.Script.Serialization.JavaScriptSerializer:
http://msdn.microsoft.com/en-us/library/system.web.script.serialization.javascriptserializer%28v=vs.110%29.aspx[^].

Each of of the methods JavaScriptSerializer tries to deserialize the JSON string into a .NET object. Please see the code samples for further detail. See also this sample:
http://dailydotnettips.com/2013/09/26/sending-raw-json-request-to-asp-net-from-jquery[^].
The key phrase on this page is: "To parse the json request, you need to parse it yourself." However, this is not exactly so. You can reasonably "guess" the expected type of a .NET object; then the deserialization with this serializer will be successful (please see my code sample demonstrating it, the second one).

The further detail depend on the type of newuser and on what you want to do with that on the server side. Let's consider a simple example:
using Serializer = System.Web.Script.Serialization.JavaScriptSerializer; 
// (reference assembly System.Web.Extensions

// ...

string jsArray = @"[1,""false"",false]";
string jsDictionary = @"{""x"": 5}";
Serializer serializer = new Serializer();
object array = serializer.DeserializeObject(jsArray);
object dictionary = serializer.DeserializeObject(jsDictionary);



要了解如何从JSON获取输入字符串,请参阅:

HTTPS://developer.moz illa.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/JSON/stringify [ ^ ]。



现在,检查对象数组字典。如果你对它们调用 GetType(),你会看到一个是 System.Object 的数组,另一个是是对象的字典,x是字符串键。这些对象过于通用,因此您可以尝试将它们解析为某些命名类型。这样,您就可以访问某些命名类型及其成员的实例。让我们重写前面的代码示例是这样的打字方式:


To understand how input strings were obtained from JSON, please see:
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/JSON/stringify[^].

Now, inspect the objects array and dictionary. If you call GetType() on them, you will see that one is an array of System.Object and another one is a dictionary of objects, and "x" is a string key. Such objects are too generic, so you can try to parse them into some named types. This way, you will be able to access the instances of some named types and their members. Let's rewrite the previous code sample is such "typed" way:

using Serializer = System.Web.Script.Serialization.JavaScriptSerializer;
using IntDictionary = System.Collections.Generic.Dictionary<string, int>;

// ...

string jsArray = @"[1,""false"",false]";
string jsDictionary = @"{""x"": 5}";
Serializer serializer = new Serializer();
object[] typedArray = serializer.Deserialize&lt;object[]>(jsArray);
IntDictionary typedDictionary = serializer.Deserialize<IntDictionary>(jsDictionary);



现在你可以使用明确定义类型的实例 object [] 和< cod> IntDictionary并实际访问实例的实例成员 typedArray typedDictionary



-SA


Now you can use instances of explicitly defined types object[] and <cod>IntDictionary and actually access the instance members of the instances typedArray and typedDictionary.

—SA


这篇关于如何将对象类型转换为C#类对象类型的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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