How can I parse a JSON file via Plugin API?
Any workaround?
Moderator: kfury77
// Parse JSON response
SC = CreateOleObject("MSScriptControl.ScriptControl");
SC.Language = "JScript";
SC.AddCode("Object.prototype.getProp=function(i){return this[i]}");
var objJson = SC.Eval("(" + xhr.responseText + ")");
var resObj = objJson.getProp("results");
var len = resObj.length;
for (var i=0;i<len;i++) {
var item = resObj.getProp(i);
var cdnUri = item.latest;
}
function OnWebkitConsoleMessage(Sender, Browser, message, source, line, Res) {
Script.Message(_t(line) + ":" + message);
}
// load htmlfile COM object
var htmlfile = CreateOleObject("htmlfile");
// force htmlfile to load Chakra engine
htmlfile.write("<meta http-equiv='x-ua-compatible' content='IE=9' />");
// Copy htmlfile COM object's JSON object and methods into "JSON" var
var JSON = htmlfile.parentWindow.JSON;
// Unload the now unneeded COM object
htmlfile.close();
// Test string
var jsonString = "{\"foo\":\"bar\",\"arr\":[7,66,88,9,6]}";
// Parse the JSON string
var jsonObj = JSON.parse(jsonString);
// Show value of the "foo" object
Script.Message("foo: " + _t(jsonObj.foo) );
/**
* JSON parser using "htmlfile" OLE object.
* The JSON result object is extended with two custom methods, making data fully
* accessible from FastScript. Custom methods:
* getProp(key/index) to access properties by index or name
* getKeys(dummy) to get list of keys
*
* @param string jsonStr The JSON string to parse
*
* @return mixed variant or empty string if failure
*/
function ParseJson(jsonStr) {
// Create htmlfile COM object
var HFO = CreateOleObject("htmlfile"), jsonObj;
// force htmlfile to load Chakra engine
HFO.write("<meta http-equiv='x-ua-compatible' content='IE=9' />");
// Add custom method to objects so data can be accessed by index and name
HFO.write("<script type='text/javascript'>Object.prototype.getProp=function(t){return this[t]},Object.prototype.getKeys=function(){return Object.keys(this)};</script>");
// Parse JSON string
try jsonObj = HFO.parentWindow.JSON.parse(jsonStr);
except jsonObj = ""; // JSON parse error
// Unload COM object
HFO.close();
return jsonObj;
}
function testJsonParser() {
Script.ClearMessages;
var jsonString = "{\"foo\":\"bar\",\"foo2\":28,\"arr\":[7,88,9,6]}";
var jsonObj = jsonParse(jsonString);
if (Length(jsonObj) == 0) {
Script.Message("Parse error");
return;
}
// Test of accessing keys, using custom methods getKeys and getProp
var keys = jsonObj.getKeys(0); // Must have a dummy parameter to work.
if (keys.length) {
for (var r=0;r<keys.length;r++) {
Script.Message("Key:" + keys.getProp(r));
}
}
// Test of accessing data by key, using dot notation
var foo = jsonObj.foo;
Script.Message("foo: " + _t(foo) );
// Test of accessing data by key, using custom getProp method
var foo2 = jsonObj.getProp("foo2");
Script.Message("foo2: " + _t(foo2) );
// Test of accessing data by index, using custom getProp method
var arr = jsonObj.getProp("arr");
for (var k=0; k < arr.length ; k++) {
Script.Message("arr[" + _t(k) + "]: " + _t(arr.getProp(k)) );
}
return;
}
// Create htmlfile COM object
var HFO = CreateOleObject("htmlfile"), res;
// force htmlfile to load Chakra engine
HFO.write("<meta http-equiv='x-ua-compatible' content='IE=9' />");
// Add custom JavaScript function
HFO.write("<script type='text/javascript'>var realJS = function(x) {return \'[\'+(x*x)+\']\';}</script>");
// Call custom JavaScript function
res = HFO.parentWindow.realJS(5); // Must have a parameter
Script.Message(res);
/**
* JSON parser using "htmlfile" OLE object.
* The JSON result object is extended with two custom methods, making data fully
* accessible from FastScript. Custom methods:
* getProp(key/index) to access properties by index or name
* getKeys(dummy) to get list of keys
*
* @param string jsonStr The JSON string to parse
*
* @return mixed variant or empty string if failure
*/
function ParseJson(jsonStr) {
// Create htmlfile COM object
var HFO = CreateOleObject("htmlfile"), jsonObj;
// force htmlfile to load Chakra engine
HFO.write("<meta http-equiv='x-ua-compatible' content='IE=9' />");
// Add custom method to objects so data can be accessed by index and name
HFO.write("<script type='text/javascript'>Object.prototype.getProp=function(t){return this[t]},Object.prototype.getKeys=function(){return Object.keys(this)};</script>");
// Parse JSON string
try jsonObj = HFO.parentWindow.JSON.parse(jsonStr);
except jsonObj = ""; // JSON parse error
// Unload COM object
HFO.close();
return jsonObj;
}
if (IsNull(_jsonApiWebkitWrapper)) {
_jsonApiWebkitWrapper = new TForm(WeBuilder);
_jsonApiWebkitWrapper.Width = 180;
_jsonApiWebkitWrapper.Height = 85;
_jsonApiWebkitWrapper.Position = poScreenCenter;
_jsonApiWebKit = Script.CreateScriptableWebkit(_jsonApiWebkitWrapper, Script.GetPath + "json_api/json.htm", "");
_jsonApiWebKit.OnLoadEnd = &json_api_onload_action;
_jsonApiWebKit.Subscribe("WebkitJsonAPI", &json_api_on_webkit_data);
var PlaceholderLabel = new TLabel(_jsonApiWebkitWrapper);
PlaceholderLabel.Parent = _jsonApiWebkitWrapper;
PlaceholderLabel.Caption = "Parsing Json file...";
PlaceholderLabel.SetBounds(15, 15, 165, 25);
} else {
_jsonApiWebKit.WebKit.Load(Script.GetPath + "json_api/json.htm");
}
_jsonApiWebkitWrapper.ShowModal;
//delete _jsonApiWebKit;
//delete _jsonApiWebkitWrapper;
Return to HTMLPad / Rapid CSS / Rapid PHP / WeBuilder Support
Users browsing this forum: No registered users and 5 guests