gpt4 book ai didi

javascript - 需要从 json 创建 javascript 数组

转载 作者:行者123 更新时间:2023-12-02 18:57:54 24 4
gpt4 key购买 nike

我需要帮助将此 json 字符串转换为 JavaScript 数组。

我尝试过这样的事情:

var cityState = $.map(source, function (value) { return value; });

其中“source”是此 ajax 调用的结果:

$.ajax({
url: 'http://localhost:49858/Default.aspx/getstuff',
dataType: 'json',
type: "POST",
data: "{}",
contentType: "application/json; charset=utf-8"
}).done(function (source) { ...

但它只是无法正常工作。

 {"query": "Unit","suggestions":[{"value":"FAIRFIELD, CONNECTICUT","data":"FAIRFIELD, CONNECTICUT"},{"value":"LONG BEACH, CALIFORNIA","data":"LONG BEACH, CALIFORNIA"},{"value":"NEW YORK, NEW YORK","data":"NEW YORK, NEW YORK"},{"value":"HONOLULU, HAWAII","data":"HONOLULU, HAWAII"},{"value":"KANSAS CITY, MISSOURI","data":"KANSAS CITY, MISSOURI"},{"value":"SAN JOSE, CALIFORNIA","data":"SAN JOSE, CALIFORNIA"},{"value":"SOUTH LAKE TAHOE, CALIFORNIA","data":"SOUTH LAKE TAHOE, CALIFORNIA"},{"value":"LAKE DALLAS, TEXAS","data":"LAKE DALLAS, TEXAS"},{"value":"BROOMFIELD, COLORADO","data":"BROOMFIELD, COLORADO"},{"value":"BROOMFIELD, COLORADO","data":"BROOMFIELD, COLORADO"}]}

console.log(源)的屏幕截图显示: enter image description here

最佳答案

这是你得到的一个奇怪的物体。看起来您将某个对象编码为 JSON,将其设置为对象属性的值,然后再次编码。

如果您想获取 suggestions 数组中所有 value 的数组,那么您可以这样做

var values = $.map(JSON.parse(source.d).suggestions, function(v){ return v.value });

关于javascript - 需要从 json 创建 javascript 数组,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15145233/

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