gpt4 book ai didi

javascript - 如何在javascript中完全获取带有重复键的JSON

转载 作者:搜寻专家 更新时间:2023-10-31 08:28:14 26 4
gpt4 key购买 nike

我试图从 url 获取 JSON,但在响应对象中删除了重复的键。有没有办法在不删除重复键的情况下完全获取它?这是我的js代码

$('document').ready(function(){
var s = $.getJSON("new.json");
console.log(s);
});

以下是我的new.json

{
"s": "wae",
"s": "asd"
}

但在控制台中我得到如下的 json 对象

responseJSON: Object
s: "asd"

提前致谢

最佳答案

如果您无法更改服务器响应,对于简单的 JSON 数据,您可以像文本一样请求 json 并像字符串一样解析它:

var check = new RegExp('["\']([^\'"]*)[\'"][^:]*:[^"\']*["\']([^\'"]*)[\'"]',"g");
$.ajax({
url : "text.json",
dataType : "text",
success : function(data){
var newData = {};
data.replace(check,function(a,b,c){
if(typeof newData[b] == "undefined"){
newData[b] = c;
}else if(typeof newData[b] == "object"){
newData[b].push(c);
}else{
var ca = newData[b];
newData[b] = [ca,c];
}
return a;
});
console.log(newData);
console.log($.parseJSON(data));
},
error : function(e,a){
console.log(e,a);
}
});

在这段代码 newData 中,你的 json 是:

{"s": ["wae","asd"]}

关于javascript - 如何在javascript中完全获取带有重复键的JSON,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30842675/

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