栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 面试经验 > 面试问答

GWT:处理传入的JSON字符串

面试问答 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

GWT:处理传入的JSON字符串

回应RMorrisey的评论:
实际上,它更令人费解:/它看起来像这样(未经测试的代码,但您应该了解一般想法):

JSonValue jsonValue;JSonArray jsonArray;JSonObject jsonObject;JSonString jsonString;jsonValue = JSONParser.parseStrict(incomingJsonRespone);// parseStrict is available in GWT >=2.1// But without it, GWT is just internally calling eval()// which is strongly discouraged for untrusted sourcesif ((jsonObject = jsonValue.isObject()) == null) {    Window.alert("Error parsing the JSON");    // Possibilites: error during download,    // someone trying to break the application, etc.}jsonValue = jsonObject.get("d"); // Actually, this needs// a null check tooif ((jsonArray = jsonValue.isArray()) == null) {    Window.alert("Error parsing the JSON");}jsonValue = jsonArray.get(0);if ((jsonObject = jsonValue.isObject()) == null) {    Window.alert("Error parsing the JSON");}jsonValue = jsonObject.get("Desc");if ((jsonString = jsonValue.isString()) == null) {    Window.alert("Error parsing the JSON");}Window.alert(jsonString.stringValue()); // Finally!

如您所见,在使用

JSONParser
时必须/应该非常谨慎-
就是要紧了,对吗?要解析不安全的JSON(否则,就像我在注释中建议的那样,您应该使用Javascript Overlay
Types)。您得到一个
JSONValue
,检查它是否确实是您认为的样子,例如,
JSONObject
得到一个
JSONObject
,检查它是否具有“
xyz”键,得到一个
JSONValue
,冲洗并重复。不是最有趣的工作,但至少比调用
eval()
整个JSON 更安全:)
注意: 正如Jason指出的那样,在GWT
2.1之前,它在内部
JSONParser
使用
eval()
(它只有一个
parse()
方法-GWT 2.0
javadocs与GWT 2.1)。在GWT
2.1中,
parse()
弃用,并引入了另外两种方法-
parseLenient()
eval()
内部使用)和
parseStrict()
(安全方法)。如果您确实需要使用
JSONParser
,那么我建议您升级到GWT
2.1
M2,因为否则您可能会使用JSO。作为
JSONParser
不受信任来源的替代方法,您可以尝试通过JSNIjson2.js集成为JSON解析器。

PS:cinqoTimo,

JSonArray jsonValue =JSONParser.parse(incomingJsonRespone);
显然不起作用,因为
JSONParser.parse
返回类型为
JSONValue
,而不是
JSONArray
-您的IDE(Eclipse+ Google Plugin?)没有警告您吗?或至少是编译器。



转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/570347.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号