cant access from JSON.Net object properties, items

Mar 28, 2016 at 6:28 PM
hi,

have a json array and it will be converted to object by JSON.Net. But when im trying to sue it with;

obj[0]

it fails with result of undefined
Coordinator
Mar 28, 2016 at 7:12 PM
Hi furesoft,

Please provide more info. A code sample would be great. We are not aware of any issues accessing script arrays from the host or vice versa.

Thanks!
Mar 30, 2016 at 3:57 PM
json:
[
{"content", "hello world"}
]
var j = Json.parse(result);

$alert(j[0],content);
Coordinator
Mar 30, 2016 at 5:53 PM
Hi again,

There are several issues with the above. The JSON sample isn't proper JSON, and the code sample (assuming it's JavaScript) seems to have at least one typo and is unlikely to work unless Json is a reference to a custom object of some kind.

Please refer to the working sample below, which uses the console for output (since we're not sure what $alert is):
engine.AddHostType(typeof(Console));
engine.Script.result = "[{\"content\": \"hello world\"}]";
engine.Execute(@"
    var j = JSON.parse(result);
    Console.WriteLine(j[0].content);
");
This sample writes "hello world" to the console. Note that if you're using JScript, you'll need WindowsScriptEngineFlags.EnableStandardsMode to enable the standard JSON object.

Good luck!