繁体   English   中英

使用 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餐厅","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":"C ity","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" :true,"pageSize":10,"radius":1,"currentPage":1,"units":"m","maxMatches":10}}"

我有使用 DOJO-AJAX 和 CORS 发送和接收消息的限制,但由于某种原因,它作为一个纯字符串出现,我无法使用以下代码在此对象中循环:

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

                    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] 中,第一个对象是“””引号。我已经尝试使用回复访问孩子。searchResults但它说未定义的对象。

谢谢

我不确定这里是否有足够的上下文来给出理想的答案,但我会立即指出该字符串已经是 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]
    }
});

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM