array("phweb" => "yes", "phemail" => "yesss")); ec-6ren">
gpt4 book ai didi

javascript - 获取 json 编码响应中的数组键

转载 作者:行者123 更新时间:2023-11-28 07:31:38 25 4
gpt4 key购买 nike

我有这个 json 编码数组

请求解析器.php

 $array = array("ph" => array("phweb" => "yes", "phemail" => "yesss"));
echo json_encode($array);

以及用于发送和处理返回响应的ajax post类型请求。

$.ajax({
type: 'POST',
url: 'requestparser.php',
data: { "request" : "pull" },
contentType: "application/json; charset=utf-8",
dataType: 'json',
cache: false,
success: function(result) {
alert(result["ph"]["phweb"]);
alert(result["ph"]["phemail"]);
}
});

我想做的是获取数组键并使用 if 语句对其进行过滤,例如(请参阅下文)

var thearraykey = array key
if (thearraykey === "ph"){
alert(array key)
}

如何从 requestparser.php 获取 json 编码响应中的数组键?任何帮助、想法和线索将不胜感激。

最佳答案

使用Object.keys函数。就像这样:

var keys = Object.keys(jsonResponse);

返回一个键数组。然后由您来迭代数组并使用键执行您想要的操作。使用后续 JSON 对象递归调用该函数将允许您检索所有 key 。

关于javascript - 获取 json 编码响应中的数组键,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29062581/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com