SampleAppDelegate.h
#import <Cocoa/Cocoa.h>
#import <WebKit/WebKit.h>
@interface SampleAppDelegate : NSObject <NSApplicationDelegate> {
NSWindow *window;
}
@property (assign) IBOutlet NSWindow *window;
+ (id)structueWithWebScriptObject:(WebScriptObject *)obj
webScriptObject:(WebScriptObject *)scriptObj;
@end
SampleAppDelegate.m
#import "SampleAppDelegate.h"
@implementation SampleAppDelegate
@synthesize window;
+ (NSDictionary *)dictionaryWithWebScriptObject:(WebScriptObject *)obj
webScriptObject:(WebScriptObject *)scriptObj
{
NSMutableDictionary *ret= [NSMutableDictionary dictionary];
id keys= [scriptObj callWebScriptMethod:@"_f0_"
withArguments:[NSArray arrayWithObjects:obj, nil]];
NSArray *keyAry= [SampleAppDelegate structueWithWebScriptObject:keys
webScriptObject:scriptObj];
unsigned i;
for(i= 0; i<[keyAry count]; i++) {
NSString *key= [keyAry objectAtIndex:i];
id val= [obj valueForKey:key];
if ([val isMemberOfClass:[WebScriptObject class]]) {
val= [SampleAppDelegate structueWithWebScriptObject:val
webScriptObject:scriptObj];
}
[ret setObject:val forKey:key];
}
return [[ret retain] autorelease];
}
+ (id)structueWithWebScriptObject:(WebScriptObject *)obj
webScriptObject:(WebScriptObject *)scriptObj
{
@try{
NSMutableArray *ret = [NSMutableArray array];
NSUInteger count = [[obj valueForKey:@"length"] integerValue]; //exception occure if associative array
unsigned i;
for (i = 0; i < count; i++) {
id val= [obj webScriptValueAtIndex:i];
if ([val isMemberOfClass:[WebScriptObject class]]) {
val= [SampleAppDelegate structueWithWebScriptObject:val
webScriptObject:scriptObj];
}
[ret addObject:val];
}
return [[ret retain] autorelease];
}
@catch (NSException *e) {
return [SampleAppDelegate dictionaryWithWebScriptObject:obj
webScriptObject:scriptObj];
}
}
- (void)applicationDidFinishLaunching:(NSNotification *)aNotification
{
WebView *webView = [[WebView alloc] init];
[[webView mainFrame] loadHTMLString:@"" baseURL:NULL];
WebScriptObject *scriptObject = [webView windowScriptObject];
NSString *_f0_= @"function _f0_(hash){var ret=[];for(var key in hash){ret.push(key);}return ret;}";
NSString *returnValue= @"[{'key0':['val000', 'val001'], 'key1':'val01'}, {'key0':'val10', 'key1':['val110', 'val111']}]";
NSString *code= [NSString stringWithFormat:@"function main(){return %@;}", returnValue];
NSString *script = [NSString stringWithFormat:@"try { %@ %@ } catch (e) { e.toString() }", code, _f0_];
id ret0= [scriptObject evaluateWebScript:script];
if (![ret0 isMemberOfClass:[WebUndefined class]]) {
NSLog(@"ret(main): %@", ret0);
}
id ret= [scriptObject callWebScriptMethod:@"main" withArguments:nil];
if (ret && [ret isMemberOfClass:[WebScriptObject class]]) {
WebScriptObject *obj= (WebScriptObject *)ret;
NSArray *nsRet= [SampleAppDelegate structueWithWebScriptObject:obj
webScriptObject:scriptObject];
NSLog(@"%@", nsRet);
}
}
@end
Result
Javascript
[{'key0':['val000', 'val001'], 'key1':'val01'}, {'key0':'val10', 'key1':['val110', 'val111']}]
Cocoa
(
{
key0 = (
val000,
val001
);
key1 = val01;
},
{
key0 = val10;
key1 = (
val110,
val111
);
}
)
0 件のコメント:
コメントを投稿