gpt4 book ai didi

javascript - 如果包含 "Most_Likely",如何拆分 JSON 值?

转载 作者:行者123 更新时间:2023-11-30 17:08:56 24 4
gpt4 key购买 nike

我正在尝试解析 JSON 值并解析我需要的内容。

基本上,我只想捕获表示为“Most_Likely”的值。

此外,如果有 OR 语句,我需要将那些值分开。

这是我尝试执行此操作的尝试,但出现 JS 错误:

for (i = 0; i < obj1.Summary.length; i++) {


for (j in obj1.Summary[i]) {

if (j.indexOf('Most_Likely') && obj1.Summary[i][j].indexOf('|OR|') >= 0){
var tempvalue = obj1.Summary[i][j].indexOf('Most_Likely').split(" |OR| ");
attributesvalues.push(tempvalue);
alert(attributesvalues);
}
else{
//do nothing
}


}

}

//示例 JSON

var obj1 = {
"Summary" :
[
{
"host:Most_Likely" : "www.google.com",
"host:Indicative" : "www.yahoo.com |OR| www.google.com",
"term:Most_Likely" : "cars" |OR| new cars |OR| SUVs,
"term:Indicative" : "automatic |OR| Lexus |OR| SUVs |OR| Civic"
}
]
};

最佳答案

这行错误

var tempvalue = obj1.Summary[i][j].indexOf('Most_Likely').split(" |OR| ");

indexOf 返回位置(类型编号),在 String 对象中拆分,这就是您出错的原因。

var attributesvalues = [],
tempvalue;

for (var i = 0; i < obj1.Summary.length; i++) {
for (var j in obj1.Summary[i]) {
if (j.indexOf('Most_Likely') >= 0 && obj1.Summary[i][j].indexOf('|OR|') >= 0) {
tempvalue = obj1.Summary[i][j].split(" |OR| ");
attributesvalues.push(tempvalue);
} else {
}
}
}

console.log(attributesvalues);

关于javascript - 如果包含 "Most_Likely",如何拆分 JSON 值?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27386151/

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