2017-09-15 125 views
0

你好我正在从我的curl函数中得到一个非常长的字符串,我想要格式化成一个数组。到目前为止,我已经把它变成了一个字符串数组,但它不完全是我想要的。我希望它被格式化为一组键和值。格式化curl返回数据

我的PHP:

 <?php 
    require 'vendor/autoload.php'; 
    $postcode = $_POST['postcode']; 
    $housenumber = $_POST['housenumber']; 
    $curl = curl_init(); 
    curl_setopt_array($curl, array(
     CURLOPT_URL => "https://api-sandbox.postnl.nl/shipment/v2_1/locations/nearest?CountryCode=NL&PostalCode=4301AC", 
     CURLOPT_RETURNTRANSFER => true, 
     CURLOPT_ENCODING => "", 
     CURLOPT_MAXREDIRS => 10, 
     CURLOPT_TIMEOUT => 30, 
     CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1, 
     CURLOPT_CUSTOMREQUEST => "GET", 
     CURLOPT_HTTPHEADER => array(
      "apikey: ozxpdJGY0wu4Li64ovzrX1G1Hs5CjRiZ", 
      "cache-control: no-cache", 
      "content-type: application/x-www-form-urlencoded", 
      "postman-token: 49b70ffc-7268-1b3a-9646-9002a290f7fe" 
     ), 
    )); 
    $response = curl_exec($curl); 
    $err = curl_error($curl); 
    curl_close($curl); 
    if ($err) { 
     echo "cURL Error #:" . $err; 
    } else { 
     echo json_encode($response); 
    } 
    ?> 

我的javascript:

function getPickUp(){ 
    var array=[]; 
    var postcode = document.getElementById('postcode').value; 
    var housenumber = document.getElementById('housenumber').value; 
     $.ajax({ 
      url: "PostNLGetPickUp.php", 
      data: {postcode: postcode, housenumber:housenumber}, 
      type: 'POST', 
      dataType: "json", 
      success: function(output) { 
; 
       x = (output.toString().slice(2,-2)); 
       x=x + ''; 
       x= x.substring(42); 
       x= x.split('},{'); 
       for(var i=0; i< x.length ; i++) 
       { 
        x[i]= x[i].replace('"',''); 
        x[i]= x[i].replace('"',''); 
        array.push([x[i]]); 
       } 
       console.log(array); 
      }, 
      error: function() { 
       alert('Something went wrong'); 
      } 
     }); 


} 

由卷曲返回的数据:

{ 
"GetLocationsResult": { 
    "ResponseLocation": [ 
     { 
      "Address": { 
       "City": "Steenwijk", 
       "Countrycode": "NL", 
       "HouseNr": 2, 
       "Remark": "Dit is een Business Point. Post en pakketten die u op werkdagen vóór de lichtingstijd afgeeft, bezorgen we binnen Nederland de volgende dag. Pakketten die u op zaterdag voor 16:00 uur afgeeft worden maandag bezorgd.", 
       "Street": "Koematen", 
       "Zipcode": "8331TK" 
      }, 
      "DeliveryOptions": { 
       "string": [ 
        "DO", 
        "PG", 
        "PGE", 
        "UL", 
        "RETA" 
       ] 
      }, 
      "Distance": 408, 
      "Latitude": 52.7762318034735, 
      "LocationCode": 172743, 
      "Longitude": 6.09661444589443, 
      "Name": "Formido", 
      "OpeningHours": { 
       "Friday": { 
        "string": "08:00-21:00" 
       }, 
       "Monday": { 
        "string": "08:00-21:00" 
       }, 
       "Saturday": { 
        "string": "08:00-18:00" 
       }, 
       "Thursday": { 
        "string": "08:00-21:00" 
       }, 
       "Tuesday": { 
        "string": "08:00-21:00" 
       }, 
       "Wednesday": { 
        "string": "08:00-21:00" 
       } 
      }, 
      "PartnerName": "PostNL", 
      "PhoneNumber": "0521-512293", 
      "RetailNetworkID": "PNPNL-01", 
      "Saleschannel": "BUPO RET", 
      "TerminalType": "NRS" 
     }, 
     { 
      "Address": { 
       "City": "Steenwijk", 
       "Countrycode": "NL", 
       "HouseNr": 4, 
       "Remark": "Dit is een Pakketpunt. Pakketten die u op werkdagen vóór lichtingstijd afgeeft, bezorgen we binnen Nederland de volgende dag.", 
       "Street": "Broekslagen", 
       "Zipcode": "8331TJ" 
      }, 
      "DeliveryOptions": { 
       "string": [ 
        "DO", 
        "PG" 
       ] 
      }, 
      "Distance": 515, 
      "Latitude": 52.779139711098, 
      "LocationCode": 172016, 
      "Longitude": 6.09575747888744, 
      "Name": "Karwei", 
      "OpeningHours": { 
       "Friday": { 
        "string": "09:00-21:00" 
       }, 
       "Monday": { 
        "string": "09:00-21:00" 
       }, 
       "Saturday": { 
        "string": "09:00-18:00" 
       }, 
       "Thursday": { 
        "string": "09:00-21:00" 
       }, 
       "Tuesday": { 
        "string": "09:00-21:00" 
       }, 
       "Wednesday": { 
        "string": "09:00-21:00" 
       } 
      }, 
      "PartnerName": "PostNL", 
      "PhoneNumber": "0521-517393", 
      "RetailNetworkID": "PNPNL-01" 
     }]}} 

这是我设法使它看起来像

Image

回答

1

任何您无法通过Javascript调用JSON.parse(data)的原因?这会将字符串转换为JSON对象。

然后,您可以从该对象

const parsedJSON = JSON.parse(output); 
console.log(parsedJSON.GetLocationsResult.ResponseLocation); 

这将注销,我认为你正试图获得地址内访问该位置阵列?

+0

是的,这个解决了我!谢谢! –

0

如果在您的JavaScript中收到JSON字符串,您可以简单地使用这样的JSON.parse() function

x = JSON.parse(output);