I am receiving the next JSON response

    {
    "timetables":[
        {"id":87,"content":"B","language":"English","code":"en"},                                                
        {"id":87,"content":"a","language":"Castellano","code":"es"}],
    "id":6,
    "address":"C/Maestro José"
    }

I would like to achieve the next pseudo code functionality

for(var i in json) {            
    if(json[i]  is Array) {
    // Iterate the array and do stuff
    } else {
    // Do another thing
    }
}

知道吗?

推荐答案

There are other methods but, to my knowledge, this is the most reliable:

function isArray(what) {
    return Object.prototype.toString.call(what) === '[object Array]';
}

So, to apply it to your code:

for(var i in json) {                    
    if(isArray(json[i])) {
    // Iterate the array and do stuff
    } else {
    // Do another thing
    }
}

Json相关问答推荐

最新版本的Deneb在数据溢出时不支持滚动

如何使用JQ有条件 Select 值

在解码的JSON哈希中搜索数组元素

Postgres Select json数组并重新映射属性名称

Oracle JSON 查询中的动态列列表

将 json 转换为 jsonb 安全吗?

将 json 文件转换为 json 对象会打乱对象的顺序

jq搜索特定字符串并输出对应的父值

使用 @ResponseBody 自定义 HttpMessageConverter 来做 Json 事情

Jackson 中的 readValue 和 readTree:何时使用哪个?

使用 gson 反序列化对象的特定 JSON 字段

使用 jq 从 bash 中管道分隔的键和值创建 JSON

为不同类型的项目数组正确的 JSON Schema

使用 JSON.Net 的 C# 到 JSON 序列化

在android中读取Json数组

如何转换为 D3 的 JSON 格式?

有什么方法可以判断您安装的 gulp 版本吗?

Gson 将一组数据对象转换为 json - Android

在android中使用GSON解析带有动态key和value的JSON

jq:按属性分组和键