0

In my c# class I have the following class:

public class Product
{
    public int product_id;
    public int quantity;
    public string book_cost;
}

If I serialize it with JavaScriptSerializer then I get back

{"product_id":0,"quantity":0,"book_cost":"hello"}

after setting the values.

Thats c#, but how could I serialize a javascript function to match this?

I am wanting to parse a js function into a c# class

Example.... if I had a class in c# and I wanted to send it to some other webserver, I could serialize the object and make it into JSON using

var serializer = new JavaScriptSerializer();
serializer.Serialize(new Product());

public class Product
{
   public int product_id = 0;
   public int quantity = 0;
   public string book_cost = "hello";
}

then on the other side i could have this:

var serializer = new JavaScriptSerializer();
Product p = serializer.Deserialize<Product>(products);
Console.WriteLine(p.product_id);

public class Product
{
   public int product_id;
   public int quantity;
   public string book_cost;
}

and it would print out 0 because I created a new product class with those values from the other class, however I want one side to be js (the sending side) and the other to be c# (the receiving side)

here is more of an example:

js side...

I have this function, I have set the values to this, but I would change them at runtime.

function Product(){
    this.product_id = 0;
    this.quantity = 0;
    this.book_cost = "hello";   
}

then I would want to generate from this I would want to generate

{"product_id":0,"quantity":0,"book_cost":"hello"}

Then on the other side I would want to populate the Product class that is above. I am only going to be using strings, integers, and date objects. If I serialize a DateTime object in c# it comes out to be

{"date":"\/Date(-62135596800000)\/"}
16
  • dupe: stackoverflow.com/questions/6487167/… Commented Dec 18, 2012 at 21:25
  • 4
    Can you give an example? I'm not sure what you are asking here. Commented Dec 18, 2012 at 21:27
  • 2
    @SmartLemon - I think what you're looking for is the ASP.NET Ajax Framework, though you could also use WCF. Both will serialize between JavaScript objects and C# objects, allowing you to work with data between both tiers. Commented Dec 18, 2012 at 21:30
  • 1
    Ok now I'm even more confused.. Commented Dec 18, 2012 at 21:33
  • 1
    im so confused by what you want? Commented Dec 18, 2012 at 21:34

1 Answer 1

2

You can serialize an object in JSON using the JSON.stringify method. Something like:

function Product() {
    this.product_id = 0;
    this.quantity = 0;
    this.book_cost = "hello";   
}

var p = new Product();
p.quantity = 5;
var json = JSON.stringify(p);

Example

Don't worry about date formats. Any good serializer is will be able to recognize various representations of dates. There should be no need to reformat anything by hand.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.