我正在尝试eval()一个有特殊字符的json - †并在Firefox 3.5.9中获得“未终止的字符串文字”错误 虽然在IE上同样可行。 7。 我在两个浏览器中都将字符编码设置为UTF-8。 知道为什么FF出错了吗?
在将String转换为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是
分别是ffffffe2 ffffff80 ffffffa8。 我怎么知道这些是否是有效的UTF-8字符?答案 0 :(得分:0)
尝试使用gson API for java-to-json。并且工作正常。 发布两个结果jsons。
使用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":"adnan.harunoglu@gdc.travel","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。有效!)
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":"adnan.harunoglu@gdc.travel","_sErrorMessage":"Error : Invalid Email Address"},{"_iRowNumber":2,"_iColumnNumber":12,"_sCellData":"adnan.harunoglu@gdc.travel","_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序列化时直接使用类变量而不是getter。 有什么想法吗?