How do I go from a Javascript array to a c# object?

Jun 26, 2012 at 12:17 AM

Hi there;

I'm new to Jint and I'm stock with some code in here, hope somebody can help me with that.
Basically I need to grab an array from some Javascript code and parse it's content onto a c# object.

The scenario:

I have this javascript code that returns an array, the code is pretty much like this:

var itemCol = new Array();

itemCol[0] = {
    pid: "01010101",
    Desc: "Some desc",
    avail: "Available",
    price: "$10.00"
};

itemCol[1] = {
    pid: "01010101",
    Desc: "Some desc",
    avail: "Available",
    price: "$10.00"
};

return itemCol;

Here is the c# I'm using to grab the array:

private object SerializeArray(string script)
{
	Jint.JintEngine engine = new Jint.JintEngine();
	Jint.Native.JsArray result = engine.Run(script);
	return result;
}

When I run the function it returns a Jint.Native.JsArray object and it's Length property is 27, which is the exactly number of items inside the array.

However, I have no idea how to access the items, when i try the m_data collection each item appears to be empty.

The reason why I need it is simple:

I'm parsing/extracting content from some html pages.
Some of the information I need is embedded on Javascript code so, I though I could run the code using Jint and get what i need.

Thanks in advance.

Jun 29, 2012 at 12:42 AM
Edited Jun 29, 2012 at 12:52 AM

You could use JavaScript to decipher the Object Array before converting it to a DataTable:

 

// Unwrap your objects
string unwrap = "var myList=''; for (var ic in itemCol) {";
unwrap += " myList += String.fromCharCode(13);";
unwrap += " for (var prop in itemCol[ic]) {";
unwrap += "  if (myList.charCodeAt(myList.length - 1) != 13)";
unwrap += "      myList += String.fromCharCode(9);";
unwrap += "  myList += prop + ':' + itemCol[ic][prop];";
unwrap += " }}";
engine.Run(unwrap);
string dataList = engine.Run("return myList;");
// Create the table
var dt = new System.Data.DataTable("MyData");
foreach (string line in dataList.Split(Convert.ToChar(13)))
{
	1f (line.Contains(":")) 
	{
		// Ensure table columns exist
		foreach (string pair in line.Split(Convert.ToChar(9)))
		{
			string col = pair.Split(':')[0];
			if (!dt.Columns.Contains(col)) dt.Columns.Add(col);
		}
		// Add data row
		System.Data.DataRow dr = dt.NewRow();
		foreach (string pair in line.Split(Convert.ToChar(9)))
		{
			string col = pair.Split(':')[0];
			string val = pair.Substring(pair.IndexOf(":") + 1);
			dr[col] = val;
		}
		dt.Rows.Add(dr);
	}
}
Mar 3, 2013 at 1:02 PM
Edited Mar 3, 2013 at 6:21 PM
Late to the party but for anyone googling it can be done like this:
Jint.JintEngine engine = new Jint.JintEngine();
Jint.Native.JsArray result = engine.Run(script);

for (var i = 0; i < result.Length; i++)
{
    var jsObj = result.get(i) as Jint.Native.JsObject;

    if (jsObj == null) continue;

    foreach (var kvp in jsObj)
    {
        Console.WriteLine("{0} is {1}", kvp.Key, kvp.Value);
    }
}