views:

23

answers:

2

In my ASP.NET page, I have a Javascript object, like this:

    var args = new Object();
    args.Data1 = document.getElementById("Data1").value;
    args.Data2 = document.getElementById("Data2").value;
    args.Data3 = document.getElementById("Data3").value;

The object is populated on client side using user input data. I am passing the data to a C# method, through an Ajax request:

    someObj.AjaxRequest(argsData1 + "|" + argsData2 + "|" + argsData3)

Finally, I need to obtain the data in my C# code:

   string data1 = [JS args.Data1]
   string data2 = [JS args.Data2]
   string data3 = [JS args.Data3]

My question is what's the best solution for this? As i am concatenating bits of user input, I don't think it's best to use "|" as a delimiter. Also, it's not clear to me how to actually parse the data in my C# code to populate the three variables with the original data.

A: 

Using a delimiter is always risky because what is the user uses that character in the input.

I would look at creating a JSON object and using .net to convert it to what you need:

http://www.json.org/

Serializing JSON FROM C#

Here is a link on deserialzing json objects in C#.

If you are using a delimiter here is how to parse it on the C# side:

string inputs = INPUTS;

string[] s = inputs.split('|');

string data1 = s[0];
if(s.Length > 1)
{
  string data2 = s[1];
}
if(s.Length> 2)
{
   string data3 = s[3];
}
Kevin
JSON looks like a good idea for this. I've started playing with JSON.NET - although for now i couldn't figure out yet how to deserialize to even the simplest object (I've posted a question about that here: http://stackoverflow.com/questions/2972431/json-net-deserialization-in-c-results-in-empty-object)
Alt_Doru
A: 

I like to use the tab character ("\t") as a delimiter because it's usually hard to impossible for the user to enter that character into most types of input fields.

BlueMonkMN