json eval 上未终止的字符串文字

发布于 2024-09-04 07:24:07 字数 501 浏览 3 评论 0原文

我正在尝试 eval() 具有特殊字符的 json - ∣ 并在 Firefox 3.5.9 中出现“未终止的字符串文字”错误 尽管在 IE 上同样可以正常工作。 7. 我在两个浏览器中都将字符编码设置为UTF-8。 知道为什么 FF 会出错吗?

同样在将字符串转换为 JSON 之前,我在 java 中运行了这段代码

String jsonString = //some json string having 

for(byte b : jsonString.getBytes()){
    System.out.print(Integer.toHexString(b) + " ");
}
net.sf.json.JSON jsonObject = net.sf.json.JSONSerializer.toJSON(jsonString);

,上面字符的 o/p

分别是 fffffffe2 ffffff80 fffffa8。 我如何知道这些是否是有效的 UTF-8 字符?

I am trying to eval() a json having speacial characters - 

and getting "unterminated string literal" error in Firefox 3.5.9
Although the same works fine on IE. 7.
I have set the character encoding to UTF-8 in both the browsers.
Any idea why its an error in FF?

Also right before converting the String to JSON, I ran this code in java

String jsonString = //some json string having 

for(byte b : jsonString.getBytes()){
    System.out.print(Integer.toHexString(b) + " ");
}
net.sf.json.JSON jsonObject = net.sf.json.JSONSerializer.toJSON(jsonString);

And the o/p for above characters is

ffffffe2 ffffff80 ffffffa8 respectively.
How do I know if these are valid UTF-8 characters?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

蓬勃野心 2024-09-11 07:24:08

尝试使用 gson API 进行 java-to-json。并且工作正常。
发布两个生成的 json。

使用 json-lib 生成的 JSON(给出未终止的字符串文字错误)

 var json = {"addToSessionFlag":false,"addToShoppingCart":false,"allMessageCodes":"IXMANAGER__PARSING_STATUS#","beanKey":"","cacheFlag":false,"callBackData":{"IXMANAGER_PARSER_ERROR_LIST":[{**"cellData":"
34295"**,"columnHeader":"","columnNumber":8,"errorMessage":"Error : Invalid Zip Code","rowNumber":1},{"cellData":"","columnHeader":"","columnNumber":9,"errorMessage":"Error : Invalid Contact Person","rowNumber":1},{"cellData":"+90-(0)212-4113838
","columnHeader":"","columnNumber":10,"errorMessage":"Error : Invalid Phone Numbers","rowNumber":1},{"cellData":"[email protected]","columnHeader":"","columnNumber":12,"errorMessage":"Error : Invalid Email Address","rowNumber":1},{"cellData":"41.000792,28.802633","columnHeader":"","columnNumber":14,"errorMessage":"Error : Invalid Website","rowNumber":1},{"cellData":"15:00 p.m","columnHeader":"","columnNumber":19,"errorMessage":"Error : Invalid Hotel Check-In/Check-Out Time","rowNumber":1},{"cellData":"12:00 p.m","columnHeader":"","columnNumber":20,"errorMessage":"Error : Invalid Hotel Check-In/Check-Out Time","rowNumber":1}]}};

var test = eval(json); 
alert(test); 

使用 gson 生成的 JSON(成功的 eval.work!)

 var json = {"keyId":0,"sSuccessType":"||ERR||","sSuccessMessage":"Parsing Failed\u003cBR /\u003e","_bAddToSessionFlag":false,"bCacheFlag":false,"success":false,"callBackData":{"IXMANAGER_PARSER_ERROR_LIST":[{"_iRowNumber":1,"_iColumnNumber":8,**"_sCellData":"\u202834295"**,"_sErrorMessage":"Error : Invalid Zip Code"},{"_iRowNumber":1,"_iColumnNumber":9,"_sErrorMessage":"Error : Invalid Contact Person"},{"_iRowNumber":2,"_iColumnNumber":9,"_sErrorMessage":"Error : Invalid Contact Person"},{"_iRowNumber":1,"_iColumnNumber":10,"_sCellData":"+90-(0)212-4113838\u2028","_sErrorMessage":"Error : Invalid Phone Numbers"},{"_iRowNumber":1,"_iColumnNumber":12,"_sCellData":"[email protected]","_sErrorMessage":"Error : Invalid Email Address"},{"_iRowNumber":2,"_iColumnNumber":12,"_sCellData":"[email protected]","_sErrorMessage":"Error : Invalid Email Address"},{"_iRowNumber":1,"_iColumnNumber":14,"_sCellData":"41.000792,28.802633","_sErrorMessage":"Error : Invalid Website"},{"_iRowNumber":2,"_iColumnNumber":14,"_sCellData":"40.971911,28.871362","_sErrorMessage":"Error : Invalid Website"},{"_iRowNumber":1,"_iColumnNumber":19,"_sCellData":"15:00 p.m","_sErrorMessage":"Error : Invalid Hotel Check-In/Check-Out Time"},{"_iRowNumber":2,"_iColumnNumber":19,"_sCellData":"14:00 p.m","_sErrorMessage":"Error : Invalid Hotel Check-In/Check-Out Time"},{"_iRowNumber":1,"_iColumnNumber":20,"_sCellData":"12:00 p.m","_sErrorMessage":"Error : Invalid Hotel Check-In/Check-Out Time"},{"_iRowNumber":2,"_iColumnNumber":20,"_sCellData":"12:00 p.m","_sErrorMessage":"Error : Invalid Hotel Check-In/Check-Out Time"}]}};
 var test = eval(json); 
alert(test); 

虽然这不是我的问题的完整解决方案,因为我认为gson在json序列化时直接使用类变量而不是getters。
有什么想法吗?

Tried using gson API for java-to-json. And works fine.
Posting both the resulting jsons.

Resulting JSON using json-lib (gives unterminated string literal error)

 var json = {"addToSessionFlag":false,"addToShoppingCart":false,"allMessageCodes":"IXMANAGER__PARSING_STATUS#","beanKey":"","cacheFlag":false,"callBackData":{"IXMANAGER_PARSER_ERROR_LIST":[{**"cellData":"
34295"**,"columnHeader":"","columnNumber":8,"errorMessage":"Error : Invalid Zip Code","rowNumber":1},{"cellData":"","columnHeader":"","columnNumber":9,"errorMessage":"Error : Invalid Contact Person","rowNumber":1},{"cellData":"+90-(0)212-4113838
","columnHeader":"","columnNumber":10,"errorMessage":"Error : Invalid Phone Numbers","rowNumber":1},{"cellData":"[email protected]","columnHeader":"","columnNumber":12,"errorMessage":"Error : Invalid Email Address","rowNumber":1},{"cellData":"41.000792,28.802633","columnHeader":"","columnNumber":14,"errorMessage":"Error : Invalid Website","rowNumber":1},{"cellData":"15:00 p.m","columnHeader":"","columnNumber":19,"errorMessage":"Error : Invalid Hotel Check-In/Check-Out Time","rowNumber":1},{"cellData":"12:00 p.m","columnHeader":"","columnNumber":20,"errorMessage":"Error : Invalid Hotel Check-In/Check-Out Time","rowNumber":1}]}};

var test = eval(json); 
alert(test); 

Resulting JSON using gson (succesful eval. works!)

 var json = {"keyId":0,"sSuccessType":"||ERR||","sSuccessMessage":"Parsing Failed\u003cBR /\u003e","_bAddToSessionFlag":false,"bCacheFlag":false,"success":false,"callBackData":{"IXMANAGER_PARSER_ERROR_LIST":[{"_iRowNumber":1,"_iColumnNumber":8,**"_sCellData":"\u202834295"**,"_sErrorMessage":"Error : Invalid Zip Code"},{"_iRowNumber":1,"_iColumnNumber":9,"_sErrorMessage":"Error : Invalid Contact Person"},{"_iRowNumber":2,"_iColumnNumber":9,"_sErrorMessage":"Error : Invalid Contact Person"},{"_iRowNumber":1,"_iColumnNumber":10,"_sCellData":"+90-(0)212-4113838\u2028","_sErrorMessage":"Error : Invalid Phone Numbers"},{"_iRowNumber":1,"_iColumnNumber":12,"_sCellData":"[email protected]","_sErrorMessage":"Error : Invalid Email Address"},{"_iRowNumber":2,"_iColumnNumber":12,"_sCellData":"[email protected]","_sErrorMessage":"Error : Invalid Email Address"},{"_iRowNumber":1,"_iColumnNumber":14,"_sCellData":"41.000792,28.802633","_sErrorMessage":"Error : Invalid Website"},{"_iRowNumber":2,"_iColumnNumber":14,"_sCellData":"40.971911,28.871362","_sErrorMessage":"Error : Invalid Website"},{"_iRowNumber":1,"_iColumnNumber":19,"_sCellData":"15:00 p.m","_sErrorMessage":"Error : Invalid Hotel Check-In/Check-Out Time"},{"_iRowNumber":2,"_iColumnNumber":19,"_sCellData":"14:00 p.m","_sErrorMessage":"Error : Invalid Hotel Check-In/Check-Out Time"},{"_iRowNumber":1,"_iColumnNumber":20,"_sCellData":"12:00 p.m","_sErrorMessage":"Error : Invalid Hotel Check-In/Check-Out Time"},{"_iRowNumber":2,"_iColumnNumber":20,"_sCellData":"12:00 p.m","_sErrorMessage":"Error : Invalid Hotel Check-In/Check-Out Time"}]}};
 var test = eval(json); 
alert(test); 

Although this is not the complete solution to my problem as I think gson uses class variables directly instead of getters at the time of json serialization.
Any thoughts about it?

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文