Cocos2dxJavascriptJavaBridge.evalString中带引号会导致js解析失败的问题

如题,例如evalString(callJs(text)),如果text带单双引号,会导致callJs方法执行失败,IOS端可以通过使用其他接口解决该问题
IOS解决方案:
ScriptingCore *core = ScriptingCore::getInstance();
jsval jsv = c_string_to_jsval(core->getGlobalContext(), text.UTF8String);
core->executeFunctionWithOwner(OBJECT_TO_JSVAL(core->getGlobalObject()), “callJs”, 1, &jsv);

Android端目前只有evalString一个接口,请问有什么合适方法可以解决该问题么

1赞

\ 转义吧

问题解决了,对executeFunctionWithOwner写了一个包装接口接收一个string参数,然后通过jni导出C++接口给Android用
ScriptingCore.h添加声明
bool callCocosWithString(const char *string);

ScriptingCore.cpp添加实现
bool ScriptingCore::callCocosWithString(const char *string)
{
jsval jsv = c_string_to_jsval(this->getGlobalContext(),string);
this->executeFunctionWithOwner(OBJECT_TO_JSVAL(this->getGlobalObject()),要调用的函数名, 1, &jsv);
}

CCJavascriptJavaBridge.cpp导出接口
JNIEXPORT jint JNICALL Java_org_cocos2dx_lib_Cocos2dxJavascriptJavaBridge_callCocos
(JNIEnv *env, jclass cls, jstring value)
{
bool strFlag = false;
std::string strValue = cocos2d::StringUtils::getStringUTFCharsJNI(env, value, &strFlag);
if (!strFlag)
{
CCLOG(“Cocos2dxJavascriptJavaBridge_evalString error, invalid string code”);
return 0;
}
ScriptingCore::getInstance()->callCocosWithString(strValue.c_str());
return 1;
}

我也遇到了。evalString一直返回false。求官方给解决方案

panda说的对