从那里How to check whether the given object is object or Array in JSON string我找到有用的比较,如果JSON对象数组或对象检查JSON只有一个字符串或者是一个数组
if (json instanceof Array) {
// get JSON array
} else {
// get JSON object
}
问题我有一个验证的登录表单,我也得到这样的消息:
array('password'=> array('isEmpty'=>'value is required and can not empty'));
然而有像
消息“电子邮件或密码无效”,即不是一个数组。
问题我需要这样的事情在JavaScript文件
if(json hasOnlyOneString)
{
//do something
} else { || } if(json instaceof Array){
// do another stuff
}
这将有助于提供通过网络发送的实际JSON。看起来你的“数组”实际上是一个非数组对象。 – apsillers 2014-09-04 16:11:02
这里是当我控制台登录错误消息时出现https://imageshack.us/i/iduC09Dap – user3904245 2014-09-04 16:41:32
请注意,'array('password'=> array('isEmpty'=>'值是必需的,不能为空'))'不会是JavaScript中'Array'的一个实例..它也不是一个字符串,它只是一个带有属性的对象,它包含具有属性的另一个对象。 – 2014-09-04 17:47:00