使用 DOJO-AJAX 将字符串转换为数组

Transform String to array using DOJO-AJAX

我有问题,我从外部 API 收到我无法控制的 JSON 消息,如下所示:

"{"searchResults":[{"resultNumber":1,"distance":0.06,"sourceName":"mqap.ntpois","name":"Chef Chen's Express","distanceUnit":"m","key":"1f9bcc0e-e03a-44d9-b034-c235c34997e2","fields":{"postal_code":"17101 "}},{"resultNumber":2,"distance":0.07,"sourceName":"mqap.ntpois","name":"Prive","distanceUnit" :"m","key":"ff5dcd5f-32ec-45fb-afd4-e66931ef84e2","fields":{"postal_code":"17101"}},{"resultNumber":3,"distance":0.07,"sourceName":"mqap.ntpois","name":"Bourbon Street Station","distanceUnit":"m","key":"a4f03b4d-12de-44ad-9ada-0f19beb34004","fields":{"postal_code":"17101"}},{"resultNumber":4,"distance":0.11,"sourceName":"mqap.ntpois" ,"name":"Pasquale's Restaurant","distanceUnit":"m","key":"a05c487e-d81d-48e0-87a8-718db70ec366","fields":{"postal_code": "17101"}},{"resultNumber":5,"distance":0.12,"sourceName":"mqap.ntpois","name":"Palumbos Italian Eatery","distanceUnit":"m","key":"595afcbd-f61a-41ea-be2f-753648a8b7e8","fields":{"postal_code":"17101"}},{"resultNumber":6,"distance":0.12,"sourceName":"mqap.ntpois","name":"Zias At Red Door","distanceUnit":"m","key":"1393b154-0785-4ca8-8f3a-4190ab808817","fields":{"postal_code":"17101"}},{"resultNumber":7,"distance" :0.13,"sourceName":"mqap.ntpois","name":"Dunes Mediterraneal Cuisine LLC","distanceUnit":"m","key":"3f8a43d1-7948- 4653-bdbb-31222f24676f","fields":{"postal_code":"17101"}},{"resultNumber":8,"distance":0.13,"sourceName":"mqap.ntpois","name":"Bricco","distanceUnit":"m","key":"3f2e7653-1313-4e17-b70a-9c04a31a029f","fields":{"postal_code":"17101"} },{"resultNumber":9,"distance":0.13,"sourceName":"mqap.ntpois","name":"Gingerbread Man Downtown","distanceUnit":"m","key":"2c36f5e0-801e-4f9e-91b6-7e9ae602a93d","fields":{"postal_code":"17101"}},{"resultNumber":10 ,"distance":0.14,"sourceName":"mqap.ntpois","name":"International House","distanceUnit":"m","key" :"2f328ca1-6fe4-44ec-964e-f7a6a73bfafd","fields":{"postal_code":"17101"}}],"origin":{"latLng":{"lng":-76.881821,"lat":40.259572},"adminArea4":"Dauphin County","adminArea5Type":"City","adminArea4Type":"County" ,"adminArea5":"Harrisburg","street":"","adminArea1":"US","adminArea3":"PA","type":"s","displayLatLng":{"lng":-76.881821,"lat":40.259572},"linkId":282035911,"postalCode":"", "sideOfStreet":"N","dragPoint":false,"adminArea1Type":"Country","geocodeQuality":"CITY","geocodeQualityCode": "A5XAX","adminArea3Type":"State"},"resultsCount":10,"hostedData":[{"tableName":"mqap.ntpois","extraCriteria":"group_sic_code=?","parameters":["581208"],"columnNames":["postal_code"]}],"totalPages":1,"info" :{"statusCode":0,"copyright":{"text":"© 2015 MapQuest, Inc.","imageUrl":"http://api.mqcdn.com/res/mqlogo.gif","imageAltText":"© 2015 MapQuest, Inc."},"messages":[]},"options":{"kmlStyleUrl":"http://www.mapquestapi.com/kml-default.kml","shapeFormat":"raw","ambiguities":真,"pageSize":10,"radius":1,"currentPage":1,"units":"m","maxMatches":10}}"

我有使用 DOJO-AJAX 和 CORS 来发送和接收消息的限制,但出于某种原因,它以纯字符串形式出现,我无法在此 object 中循环使用代码如下:

renderReply : function(reply, textStatus, jqXHR) { var pois = new MQA.ShapeCollection(); var html = 'IDNAMEADDRESSZIPCATEGORYDISTANCE (miles)';

                    var jsonObj = dojo.toJson(reply);

                    // add POI markers and populate the search result table
                    for (i = 0; i < reply.length; i++) {
                        var result = reply[i];

如您所见,我已经尝试使用 toJson 对其进行转换,但由于某种原因,最糟糕的是将 \ 添加到消息中,并且在回复中 [i] 第一个 object 是 " " " 引号。我已经尝试使用 reply.searchResults 访问 child 但它说未定义 object.

谢谢

我不确定这里是否有足够的上下文来给出理想的答案,但我会立即指出该字符串 already JSON,所以 dojo.toJson 正在对已经字符串化的内容进行字符串化。您需要 dojo.fromJson(或者,在较新版本的 Dojo 中最好是 dojo/json.parse)。

我还要指出的是,一旦您解析了这个 JSON 字符串,您可能想要遍历已解析对象中的 searchResults 属性,而不是对象本身。

require(['dojo/json', ...], function(JSON, ...) {
    // Assuming `reply` contains the string:
    var replyObj = JSON.parse(reply);
    var results = replyObj.searchResults;
    for (var i = 0, l = results.length; i++) {
        // Do something with results[i]
    }
});