0

我需要帮助将此 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(source) 的屏幕截图显示: 在此处输入图像描述

4

1 回答 1

2

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

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

var values = $.map(JSON.parse(source.d).suggestions, function(v){ return v.value });
于 2013-02-28T20:44:03.203 回答