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;
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:
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<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