manoj chalode
manoj chalode

Reputation: 5

convert javascript object into c# object

I am working on client side programming and I want to convert a javascript object into c# object so that I can save it in database. Is there any method to achieve this ? Thanks

Upvotes: 0

Views: 14545

Answers (3)

Jesse Carter
Jesse Carter

Reputation: 21217

First you will need to pass the JSON object to the server side possibly using a web service in code behind and then you could try this awesome library:

JSON.net

The biggest hurdle you're going to have to overcome here regardless of what serialization/deserialzation technique you decide to go with is how you're going to pass the JSON string from the client side to your code behind where you can use it. For this I recommend using jQuery's AJAX capabilities. This article proved invaluable to me when I was learning how to pass information from the client

Upvotes: 2

gudatcomputers
gudatcomputers

Reputation: 2882

For this solution to work you need the type represented by T to be of a type that is decorated with DataContract and DataMember attributes. The DataContractJsonSErializer is WAY better than JavascriptSerializer. This is from some code I wrote for a C# REST client. It does show how to serialize and deserialize generically though. Example object included

[DataContract]
public SampleObject
{
   [DataMember("MyProperty")]
   public string MyProperty {get;set;}
}

    private static byte[] SerializeRequest<T>(string contentType, T request)
    {
        using (MemoryStream requestObjectStream = new MemoryStream())
        {
            if (contentType == "applicaton/json")
            {
                DataContractJsonSerializer serializer = new DataContractJsonSerializer(typeof(T));
                serializer.WriteObject(requestObjectStream, request);
            }
            else if (contentType == "application/xml")
            {
                DataContractSerializer serializer = new DataContractSerializer(typeof(T));
                serializer.WriteObject(requestObjectStream, request);
            }
            else
            {
                throw new Exception("invalid contentType");
            }
            requestObjectStream.Position = 0;

            return requestObjectStream.ToArray();
        }
    }

    private static T DeserializeResponse<T>(string acceptHeader, string responseString)
    {            
        if (acceptHeader == "applicaton/json")
        {
            DataContractJsonSerializer serializer = new DataContractJsonSerializer(typeof(T));
            MemoryStream responseObjectStream = new MemoryStream(Encoding.UTF8.GetBytes(responseString));
            return (T)serializer.ReadObject(responseObjectStream);
        }
        else if (acceptHeader == "application/xml")
        {
            DataContractSerializer serializer = new DataContractSerializer(typeof(T));
            MemoryStream responseObjectStream = new MemoryStream(Encoding.UTF8.GetBytes(responseString));
            return (T)serializer.ReadObject(responseObjectStream);
        }

        return default(T);
    }

Upvotes: -1

Boris Gappov
Boris Gappov

Reputation: 2493

javascript:

var obj = {id:0,userName:'Bill'};

c#, define class:

public class myClass
{
  public int id;
  public string userName;
}

then transport obj to server (for example using AJAX request), and deserialize, when you need work with object

myClass obj = (new JavascriptSerializer()).Deserialize<myClass>(objJson);

But you can do without deserialization and store objJson string to database

Upvotes: 6

Related Questions