如何使用JavaScriptCore将javascript对象转换为NSDictionary

时间:2013-11-03 02:31:26

标签: javascript objective-c javascriptcore

假设我有一个javascript文件javascript.js,其中包含以下内容。

window.fruitsAndVeggies = {
    name2CategoryMap: {
        "apple": "fruit",
        "carrot": "vegetable"
    }
}

有人能告诉我将Javascript对象window.fruitsAndVeggies的内容放入NSDictionary的最简单方法吗?

从互联网上的各种来源我拼凑了以下代码片段,它创建了一个Javascript上下文,然后在该上下文中评估了javascript代码。

JSGlobalContextRef ctx = JSGlobalContextCreate(NULL);  // create context


JSValueRef exception = JSValueMakeUndefined(ctx); // create object to hold exceptions

// Make a "window" object in the JS Context
JSStringRef makeWindowScript = JSStringCreateWithUTF8CString("var window = {}");
JSValueRef result = JSEvaluateScript( ctx, makeWindowScript, NULL, NULL, 0, &exception );


// load the javascript file into an NSString
NSBundle *          bundle = [NSBundle bundleWithIdentifier:@"my.bundle"];
NSString *filePath = [bundle pathForResource:@"javascript" ofType:@"js"];

NSError *error;
NSString *stringFromFile = [[NSString alloc]
                                 initWithContentsOfFile:filePath
                                 encoding:NSUTF8StringEncoding
                                 error:&error];

// convert NSString to a JSStringRef
CFStringRef cfString = (__bridge CFStringRef)stringFromFile;
JSStringRef jsString = JSStringCreateWithCFString(cfString);


// evaluate the javascript
JSEvaluateScript( ctx, jsString, NULL, NULL, 0, &exception );

我对下一步做什么感到困惑。我需要在我的objective-c代码中使用fruitsAndVeggies.name2CategoryMap的内容。访问它们最简单的方法是什么?我可以调用一个简单的函数将它们加载到objective-c字典中吗?

非常感谢你的帮助。

2 个答案:

答案 0 :(得分:0)

尽我所能,我无法找到一种优雅的方式将window.fruitsAndVeggies放入字典中。我能做的最好的事情是手动滚动循环遍历javascript对象的循环,并逐个将每个键/值对添加到字典中。

代码如下。警告:它很难看。如果你有一个更简洁的解决方案,我会全力以赴!

/* Make the dictionary. */
NSMutableDictionary *fruitsAndVeggiesDict = [NSMutableDictionary dictionary];


/* In the JS context, create a variable called "keys". 
   Return the number of keys (keys.length).  */
result = JSEvaluateScript( ctx, JSStringCreateWithUTF8CString("var keys = Object.keys(window.fruitsAndVeggies.name2CategoryMap); keys.length"), NULL, NULL, 0, &exception );

double numKeys = JSValueToNumber(ctx, result, &exception);


/* Iterate over the keys; convert each key/value into a pair of NSStrings. */
for(int i = 0; i < numKeys; i++){

    // get a key, save in keyStrNS
    NSString *getKeyStr = [NSString stringWithFormat:@"keys[%d]", i];
    CFStringRef getKeyStrCF = (__bridge CFStringRef)getKeyStr;
    JSStringRef getKeyStrJS = JSStringCreateWithCFString(getKeyStrCF);

    JSValueRef key = JSEvaluateScript( ctx, getKeyStrJS, NULL, NULL, 0, &exception );

    JSStringRef keyStrJS = JSValueToStringCopy(ctx, key, &exception);
    NSString *keyStrNS = (NSString *)JSStringCopyCFString( kCFAllocatorDefault, keyStrJS );


    // get a value, save in valueStrNS
    NSString *getValueStr = [NSString stringWithFormat:@"window.fruitsAndVeggies.name2CategoryMap[\"%@\"]", keyStrNS];
    CFStringRef getValueStrCF = (__bridge CFStringRef)getValueStr;
    JSStringRef getValueStrJS = JSStringCreateWithCFString(getValueStrCF);

    JSValueRef value = JSEvaluateScript( ctx, getValueStrJS, NULL, NULL, 0, &exception );

    JSStringRef valueStrJS = JSValueToStringCopy(ctx, value, &exception);
    NSString *valueStrNS = (NSString *)JSStringCopyCFString( kCFAllocatorDefault, valueStrJS );

    /* store the key and value in our dictionary */
    [fruitsAndVeggiesDict setObject: valueStrNS  forKey: keyStrNS];

    /* and print them for good measure */
    NSLog(@"key %@ has value %@",keyStrNS,valueStrNS);

}

输出:

key apple has value fruit
key carrot has value vegetable

答案 1 :(得分:0)

自从iOS 7引入JavaScriptCore中的新Objective-C接口以来,事情变得更加简单。有关此内容的介绍,请参阅Apple开发者网络上的WWDC 2013会话“将JavaScript集成到本机应用程序”会话:{{3 }}

评估javascript资源文件后需要的代码是:

    JSContext *context = [JSContext contextWithJSGlobalContextRef:ctx];
    NSDictionary *fruitsAndVeggiesDict = [context[@"window"][@"fruitsAndVeggies"][@"name2CategoryMap"] toObject];

    NSLog(@"name2CategoryMap['apple'] = %@", fruitsAndVeggiesDict[@"apple"]);
    NSLog(@"name2CategoryMap['carrot'] = %@", fruitsAndVeggiesDict[@"carrot"]);