gpt4 book ai didi

javascript - 从 json stringify 中提取文本

转载 作者:行者123 更新时间:2023-12-01 00:34:38 26 4
gpt4 key购买 nike

我是 JSON Javascript 新手。我有一个对象,我只想提取文本值。

这是我的代码,但是当我运行它时,结果是未定义:

    var obj = [{
"start_location":
{
"lat":47.951042801290065,
"lng":-116.70764186944393
},
"end_location":
{
"lat":47.94871454878046,
"lng":-116.70839074239734
},
"length":
{
"text":"265.13 m",
"value":265.126074613001
}
},
{
"start_location":
{
"lat":47.94871454878046,
"lng":-116.70839074239734
},
"end_location":
{
"lat":47.949763712586105,
"lng":-116.70401337728504
},
"length":
{
"text":"346.65 m",
"value":346.6461031139708
}
},
{
"start_location":
{
"lat":47.949763712586105,
"lng":-116.70401337728504
},
"end_location":
{
"lat":47.951042801290065,
"lng":-116.70764186944393
},
"length":
{
"text":"305.72 m",
"value":305.7189215338448
}
}
];

var myJSON = JSON.stringify(obj);

document.getElementById("demo").innerHTML = myJSON[['length'],['text']];

最佳答案

你为什么要对其进行字符串化。从 JS 对象中获取比 JSON 格式的字符串更容易。

var obj = [
{
"start_location": { "lat": 47.951042801290065, "lng": -116.70764186944393 },
"end_location": { "lat": 47.94871454878046, "lng": -116.70839074239734 },
"length": { "text": "265.13m", "value": 265.126074613001 }
},
{
"start_location": { "lat": 47.94871454878046, "lng": -116.70839074239734 },
"end_location": { "lat": 47.949763712586105, "lng": -116.70401337728504 },
"length": { "text": "346.65m", "value": 346.6461031139708 }
},
{
"start_location": { "lat": 47.949763712586105, "lng": -116.70401337728504 },
"end_location": { "lat": 47.951042801290065, "lng": -116.70764186944393 },
"length": { "text": "305.72m", "value": 305.7189215338448 }
}
];

let lengths = obj.map(item => item.length.text);

console.log(lengths);

关于javascript - 从 json stringify 中提取文本,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58233727/

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