2013-11-26 27 views
2
 json= new Gson().toJson(name); 

其中name是字符串类型。TypeError:无效'in'操作数目标

我得到错误说:

错误是在Java脚本的给定部分

return type === "array" || type !== "function" && 
    (length === 0 || 
    typeof length === "number" && length > 0 && (length - 1) in obj); 

我也曾尝试

response=JSON.parse(response); 

“类型错误 '在' 操作数OBJ无效”这不起作用。

+0

是什么'responseJson' –

+0

是什么'obj'? – Grundy

+0

我从servlet获取的响应对象 – user2814799

回答

2

通过链接In operator可以看到

The in operator returns true if the specified property is in the specified object.

在你的代码

(length - 1) in obj你尝试检查该属性(length - 1)该数字在你的obj

我觉得objstring,因此它具有length财产,但没有数字属性,所以你必须赶上这种情况

+0

这是许多servlets的常见java脚本,我不能改变那只是数值 – user2814799

+0

所以你可以提供什么是obj值? – Grundy

+0

那么?如果你有'var obj = {5:'foo'};'然后'obj'中的5将返回'true'。这个错误表明'obj'对'in'来说不是一个有效的操作数。 –

1

我关闭代码“(长度 - 1)在obj”之间括号括起来。

return type === "array" || type !== "function" && 
    (length === 0 || 
    typeof length === "number" && length > 0 && ((length - 1) in obj)); 

这对我来说很好。

0

在我的情况下,对象是不是空的,但我需要的对象的键,例如加双引号:

obj = {params : "paramsInputPreData"} 

上面会产生一个错误,但下面一个是好的:

obj = {"params" : "paramsInputPreData"} 

所以对我所需要的密钥被引用为“PARAMS”

希望它可以帮助别人

0

当你有这样的错误,你需要检查你的变量的类型等等,你总是可以做这样的事情:

if (typeof x == 'string' || typeof x == 'number') { ... }