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

jhkqcmku  于 2022-12-16  发布在  Dojo
关注(0)|答案(1)|浏览(169)

I am having an issue, I am receiving from an external API that I don't have control a JSON message like below:
"{"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":true,"pageSize":10,"radius":1,"currentPage":1,"units":"m","maxMatches":10}}"
I have the restriction to be using DOJO-AJAX and CORS to send and receive the message but for some reason it comes as a plain string where I can't loop in this object using the code below:
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];

As you can see I already tried to convert it with toJson but for some reason is worst is adding \ to the message and in reply[i] the first object is " " " quotes. I already tried accessing to the child with reply.searchResults but it says undefined object.
Thanks

csga3l58

csga3l581#

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

相关问题