Click here to Skip to main content
15,885,914 members
Please Sign up or sign in to vote.
1.44/5 (2 votes)
See more:
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# code


C#
public int InsertUserDetails([FromBody]object datatosave)
       {
                
                MdlUser objuser = (MdlUser) (datatosave);
                return SettingManager.InsertUserDetails(objuser);
         } 
Posted
Updated 7-Sep-20 21:42pm
v3
Comments
Bernhard Hiller 7-Aug-14 9:00am    
? And your question is ... ?

1 solution

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:
C#
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);

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:
C#
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);

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
 
Share this answer
 
v8
Comments
[no name] 8-Sep-20 3:41am    
+

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900