You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
I am facing difficulty parsing JSON in JavaScript method.
My C# Code
string jsonString = “{\”Name\”: \”Ankur\”, \”Sex\”: \”Male\”}”;
var o = new IronJS.Hosting.CSharp.Context();
o.ExecuteFile(@”C:\CustomScript.js”);
var handleJson = o.Globals.GetT(“HandleJson”);
var result = handleJson.Call(o.Globals, jsonString).Unbox();
Console.WriteLine(result);
JavaScript method in CustomScript.js
function HandleJson(jsonStr) {
obj = JSON.parse(jsonStr);
return obj.Name;
}
Everytime I do this, I get error message saying “ReferenceError: JSON is not defined”
Guess, “JSON.parse” method is native to browser and isn’t available server side.
I can use jQuery method obj = $.parseJSON(jsonStr); as well but don’t know how to load jQuery file at server.
Any thoughts on what I am doing wrong or how to fix it?
Thanks.
The text was updated successfully, but these errors were encountered:
JSON.parse is an unknown JS method at Server(which is why we were getting the error)... So, we need to add/load "json2.js" before CustomScript.js file and then we will be good.
string jsonString = "{\"Name\": \"Ankur\", \"Sex\": \"Male\"}";
var o = new IronJS.Hosting.CSharp.Context();
o.ExecuteFile(@"C:\json2.js");
o.ExecuteFile(@"C:\CustomScript.js");
var handleJson = o.Globals.GetT<FunctionObject>("HandleJson");
var result = handleJson.Call(o.Globals, jsonString).Unbox<string>();
Console.WriteLine(result);
Hi,
I am facing difficulty parsing JSON in JavaScript method.
My C# Code
string jsonString = “{\”Name\”: \”Ankur\”, \”Sex\”: \”Male\”}”;
var o = new IronJS.Hosting.CSharp.Context();
o.ExecuteFile(@”C:\CustomScript.js”);
var handleJson = o.Globals.GetT(“HandleJson”);
var result = handleJson.Call(o.Globals, jsonString).Unbox();
Console.WriteLine(result);
JavaScript method in CustomScript.js
function HandleJson(jsonStr) {
obj = JSON.parse(jsonStr);
return obj.Name;
}
Everytime I do this, I get error message saying “ReferenceError: JSON is not defined”
Guess, “JSON.parse” method is native to browser and isn’t available server side.
I can use jQuery method obj = $.parseJSON(jsonStr); as well but don’t know how to load jQuery file at server.
Any thoughts on what I am doing wrong or how to fix it?
Thanks.
The text was updated successfully, but these errors were encountered: