Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/node.js/39.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
循环JSON数组并使用node.js获取值_Node.js_Arrays_Json_Parsing - Fatal编程技术网

循环JSON数组并使用node.js获取值

循环JSON数组并使用node.js获取值,node.js,arrays,json,parsing,Node.js,Arrays,Json,Parsing,我有json字符串中的http请求,我需要响应。。 我需要从字符串中获取值,以便能够返回响应 正是我需要的价值 queryResult->outputContexts->name first(项目)。 json字符串如下所示: { "responseId": "61e3f7da-b529-49e0-80-9eaae4695ec1-ee7586fb", "queryResult": { "quer

我有json字符串中的http请求,我需要响应。。 我需要从字符串中获取值,以便能够返回响应 正是我需要的价值 queryResult->outputContexts->name first(项目)。 json字符串如下所示:

{
    "responseId": "61e3f7da-b529-49e0-80-9eaae4695ec1-ee7586fb",
    "queryResult": {
        "queryText": "Get My VIP Card",
        "parameters": {
            "PARAM": ""
        },
        "allRequiredParamsPresent": true,
        "outputContexts": [
            {
                "name": "projects/mbmsoft-nehh/agent/sessions/v-2-404581/contexts/fulllllll-followup",
                "lifespanCount": 1,
                "parameters": {
                    "PARAM": "",
                    "PARAM.original": ""
                }
            },
            {
                "name": "projects/mbmsoft-nehh/agent/sessions/v-2-4024581/contexts/__system_counters__",
                "lifespanCount": 1,
                "parameters": {
                    "no-input": 0,
                    "no-match": 0,
                    "PARAM": "",
                    "PARAM.original": ""
                }
            }
        ],
        "intent": {
            "name": "projects/mbmsoft-nehh/agent/intents/74a9a-335b-46ea-817a-2cb45fcc04a5",
            "displayName": "fulllllll"
        },
        "intentDetectionConfidence": 1,
        "languageCode": "en"
    },
    "originalDetectIntentRequest": {
        "payload": {
            "source": "viber",
            "body": {
                "timestamp": 1604958504809,
                "event": "message",
                "silent": false,
                "sender": {
                    "api_version": 8,
                    "name": "john smith",
                    "country": "MK",
                    "language": "en-US",
                    "id": "as6q0NaBQjKiTPDw=="
                },
                "message": {
                    "type": "text",
                    "text": "Get My VIP Card"
                },
                "chat_hostname": "SN-CHAT-10_",
                "message_token": 5506704778261146000
            },
            "contact": {
                "cId": "as6q0NQQ+IdadsajKiTPDw=="
            }
        }
    },
    "session": "projects/mbmsoft-nehh/agent/sessions/v-2-424581"
}
exports.id_session = function () {
    const { queryResult } = response;
    const { outputContexts } = queryResult;
    const nameArray = outputContexts.map(({ name }) => {
        return name;
    });
    console.log(nameArray)
    let a, b, rest;
    [a, ...rest] = nameArray;
    console.log(a);
    return a; 
};
当我在四处阅读时,我应该循环字符串数组中的对象,但我找不到类似的示例,因为里面的对象不同。。 我需要一些基本的说明如何在node.js中循环通过json数组
非常感谢执行您希望看到的操作,因为queryResult->OutputContext是一个数组。另外,为了理解我发布的解决方案的某些部分,关于

const响应={
“响应ID:“61e3f7da-b529-49e0-80-9eaae4695ec1-ee7586fb”,
“查询结果”:{
“queryText”:“获取我的VIP卡”,
“参数”:{
“参数”:”
},
“allRequiredParamsPresent”:真,
“输出上下文”:[
{
“名称”:“projects/mbmsoft nehh/agent/sessions/v-2-404581/context/fulllllll followup”,
“生命计数”:1,
“参数”:{
“参数”:“,
“原始参数”:”
}
},
{
“名称”:“projects/mbmsoft nehh/agent/sessions/v-2-4024581/contexts/_____系统计数器”,
“生命计数”:1,
“参数”:{
“无输入”:0,
“不匹配”:0,
“参数”:“,
“原始参数”:”
}
}
],
“意图”:{
“名称”:“项目/mbmsoft nehh/agent/intents/74a9a-335b-46ea-817a-2cb45fcc04a5”,
“displayName”:“fulllllll”
},
“意向检测信心”:1,
“语言代码”:“en”
},
“原始检测任务”:{
“有效载荷”:{
“来源”:“毒蛇”,
“正文”:{
“时间戳”:1604958504809,
“事件”:“消息”,
“沉默”:假,
“发件人”:{
“api_版本”:8,
“姓名”:“约翰·史密斯”,
“国家”:“MK”,
“语言”:“en US”,
“id”:“as6q0NaBQjKiTPDw==”
},
“信息”:{
“类型”:“文本”,
“文本”:“获取我的VIP卡”
},
“聊天室主机名”:“SN-chat-10”,
“消息令牌”:5506704778261146000
},
“联系人”:{
“cId”:“as6q0NQQ+IdadsajKiTPDw=”
}
}
},
“会话”:“projects/mbmsoft nehh/agent/sessions/v-2-424581”
};
const{queryResult}=响应;
const{outputContexts}=queryResult;
outputContexts.forEach({name})=>{
console.log(名称);
});
const nameArray=outputContexts.map({name})=>{
返回名称;
});

log(nameArray)1.我按照@Nicolas Mauricio的指示,前进如下:

{
    "responseId": "61e3f7da-b529-49e0-80-9eaae4695ec1-ee7586fb",
    "queryResult": {
        "queryText": "Get My VIP Card",
        "parameters": {
            "PARAM": ""
        },
        "allRequiredParamsPresent": true,
        "outputContexts": [
            {
                "name": "projects/mbmsoft-nehh/agent/sessions/v-2-404581/contexts/fulllllll-followup",
                "lifespanCount": 1,
                "parameters": {
                    "PARAM": "",
                    "PARAM.original": ""
                }
            },
            {
                "name": "projects/mbmsoft-nehh/agent/sessions/v-2-4024581/contexts/__system_counters__",
                "lifespanCount": 1,
                "parameters": {
                    "no-input": 0,
                    "no-match": 0,
                    "PARAM": "",
                    "PARAM.original": ""
                }
            }
        ],
        "intent": {
            "name": "projects/mbmsoft-nehh/agent/intents/74a9a-335b-46ea-817a-2cb45fcc04a5",
            "displayName": "fulllllll"
        },
        "intentDetectionConfidence": 1,
        "languageCode": "en"
    },
    "originalDetectIntentRequest": {
        "payload": {
            "source": "viber",
            "body": {
                "timestamp": 1604958504809,
                "event": "message",
                "silent": false,
                "sender": {
                    "api_version": 8,
                    "name": "john smith",
                    "country": "MK",
                    "language": "en-US",
                    "id": "as6q0NaBQjKiTPDw=="
                },
                "message": {
                    "type": "text",
                    "text": "Get My VIP Card"
                },
                "chat_hostname": "SN-CHAT-10_",
                "message_token": 5506704778261146000
            },
            "contact": {
                "cId": "as6q0NQQ+IdadsajKiTPDw=="
            }
        }
    },
    "session": "projects/mbmsoft-nehh/agent/sessions/v-2-424581"
}
exports.id_session = function () {
    const { queryResult } = response;
    const { outputContexts } = queryResult;
    const nameArray = outputContexts.map(({ name }) => {
        return name;
    });
    console.log(nameArray)
    let a, b, rest;
    [a, ...rest] = nameArray;
    console.log(a);
    return a; 
};

由于我是node.js的新手,我将感谢任何能够发送其他方法从json数组中提取值的人。

与此同时,我发现了一种从json数组中获取值的非常简单的方法,如下所示:

{
    "responseId": "61e3f7da-b529-49e0-80-9eaae4695ec1-ee7586fb",
    "queryResult": {
        "queryText": "Get My VIP Card",
        "parameters": {
            "PARAM": ""
        },
        "allRequiredParamsPresent": true,
        "outputContexts": [
            {
                "name": "projects/mbmsoft-nehh/agent/sessions/v-2-404581/contexts/fulllllll-followup",
                "lifespanCount": 1,
                "parameters": {
                    "PARAM": "",
                    "PARAM.original": ""
                }
            },
            {
                "name": "projects/mbmsoft-nehh/agent/sessions/v-2-4024581/contexts/__system_counters__",
                "lifespanCount": 1,
                "parameters": {
                    "no-input": 0,
                    "no-match": 0,
                    "PARAM": "",
                    "PARAM.original": ""
                }
            }
        ],
        "intent": {
            "name": "projects/mbmsoft-nehh/agent/intents/74a9a-335b-46ea-817a-2cb45fcc04a5",
            "displayName": "fulllllll"
        },
        "intentDetectionConfidence": 1,
        "languageCode": "en"
    },
    "originalDetectIntentRequest": {
        "payload": {
            "source": "viber",
            "body": {
                "timestamp": 1604958504809,
                "event": "message",
                "silent": false,
                "sender": {
                    "api_version": 8,
                    "name": "john smith",
                    "country": "MK",
                    "language": "en-US",
                    "id": "as6q0NaBQjKiTPDw=="
                },
                "message": {
                    "type": "text",
                    "text": "Get My VIP Card"
                },
                "chat_hostname": "SN-CHAT-10_",
                "message_token": 5506704778261146000
            },
            "contact": {
                "cId": "as6q0NQQ+IdadsajKiTPDw=="
            }
        }
    },
    "session": "projects/mbmsoft-nehh/agent/sessions/v-2-424581"
}
exports.id_session = function () {
    const { queryResult } = response;
    const { outputContexts } = queryResult;
    const nameArray = outputContexts.map(({ name }) => {
        return name;
    });
    console.log(nameArray)
    let a, b, rest;
    [a, ...rest] = nameArray;
    console.log(a);
    return a; 
};
重要提示(特别是对于初学者):

若您正在接收http请求的响应,那个么响应将以文本字符串的形式出现,所以您首先需要将JSON对象转换为be abblle,以便从数组中提取值

响应的完整示例写在这个线程的开头

http request code 
...
res.on("end", function (chunk) {
    var body = Buffer.concat(chunks);
    const response = JSON.parse(body.toString()); //convert string to JSON object

    var result = response.originalDetectIntentRequest.payload.contact.cId;
    console.log(result);
    return result;
 });
或:


也许可以尝试类似的方法:queryResult.outputContexts.map(obj=>{return{name:obj.name}})。我不确定这是否是你想要的。