Quantcast
Channel: ClearScript
Viewing all articles
Browse latest Browse all 2297

New Post: Pass Javascript Object with Array to the host

$
0
0
Here is my code:
using System;
using Microsoft.ClearScript.V8;
using Newtonsoft.Json;

namespace ConsoleApplication
{
    public class Test
    {
        public void Output(object o)
        {
            Console.WriteLine(JsonConvert.SerializeObject(o));
        }
    }

    class Program
    {
        static void Main(string[] args)
        {
            var engine = new V8ScriptEngine();
            engine.AddHostObject("test", new Test());
            var script = "var o = {Persons: [{Name :\"John\"}, {Name :\"Joe\"}] }; test.Output(o);";
            engine.Execute(script);            
        }
    }
}
I expect that it output

{
"Persons": [
{
  "Name": "John"
},
{
  "Name": "Joe"
}
]
}

but it actually output

{
"Persons": {
"0": {
  "Name": "John"
},
"1": {
  "Name": "Joe"
}
}
}

I understand it use the dynamic V8ScriptItem to convert the javascript object to host object.

But is there a way to keep array of javascript as also a array or list of host object?

Viewing all articles
Browse latest Browse all 2297

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>