2016-02-04 74 views
3

我有這樣的代碼authorize.net JSON返回多餘的字符

$ch = curl_init(); 
    curl_setopt($ch, CURLOPT_URL, $url); 
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); 
    curl_setopt($ch, CURLOPT_HTTPHEADER, Array("'Content-Type: application/json; charset=utf-8'")); 
    curl_setopt($ch, CURLOPT_HEADER, 0); 
    curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($data_str)); 
    curl_setopt($ch, CURLOPT_POST, 1); 
    curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0); 
    $xmlresult = curl_exec($ch); 
    $xmlError = $xmlresult; 
    $json = json_decode($xmlresult, true); 

答案,我到JSON,但我無法轉換,因爲在一開始的回答是,多餘的漢字例如

п»ї{"customerPaymentProfileIdList":[],"customerShippingAddressIdList":[],"validationDirectResponseList":[],"messages":{"resultCode":"Error","message":[{"code":"E00039","text":"A duplicate record with ID 39223758 already exists."}]}} 

響應頭

HTTP/1.1 200 OK 緩存控制:私人 Conten t-Length:232 Content-Type:application/json;字符集= UTF-8 服務器:Microsoft-IIS/7.5 X-ASPNET-版本:2.0.50727 X供電,通過:ASP.NET 訪問控制允許來源:* 訪問控制 - 允許 - 方法:PUT,OPTIONS,POST,GET 日期:2016年2月4日,星期四09:08:15訪問控制 - 允許 - 標題:x-requested-with,cache-control,content-type,origin,method,SOAPAction GMT 連接:保持活躍

由於額外的字符我不能json_decode字符串。可以做什麼?

回答

3

我在開發我的library for accessing their JSON API時遇到同樣的問題。在the code that handles the response我不得不刪除這些字符,以正確解碼字符串爲JSON。

行113:

$this->responseJson = preg_replace('/[\x00-\x1F\x80-\xFF]/', '', $responseJson); 
+0

我看到這個表達式很多。它是否應該通過整個JSON字符串以任何順序剝離出任意數量的這些字符?它不應該只是看前三個字符嗎? '/^[\ x00- \ x1F \ x80- \ xFF] {3,3} /'' – Jason

0

我有在Node.js的同樣的問題與JSON.parse()

var https = require('https'); 
var requestData = { 
    "getCustomerProfileIdsRequest": { 
    "merchantAuthentication": { 
     "name": "your-auth-name-here", 
     "transactionKey": "your-trans-key-name-here" 
    } 
    } 
}; 
var requestString = JSON.stringify(requestData); 
var req = https.request({ 
    host: "apitest.authorize.net", 
    port: "443", 
    path: "/xml/v1/request.api", 
    method: "POST", 
    headers: { 
    "Content-Length": requestString.length, 
    "Content-Type": "application/json" 
    } 
}); 

req.on('response', function (resp) { 
    var response = ''; 

    resp.setEncoding('utf8'); 
    resp.on('data', function(chunk) { 
    response += chunk; 
    }); 
    resp.on('end', function() { 
    var buf = new Buffer(response); 
    console.log('buf[0]:', buf[0]); // 239 Binary 11101111 
    console.log('buf[0] char:', String.fromCharCode(buf[0])); // "ï" 
    console.log('buf[1]:', buf[1]); // 187 Binary 10111011 
    console.log('buf[1] char:', String.fromCharCode(buf[1])); // "»" 
    console.log('buf[2]:', buf[2]); // 191 Binary 10111111 
    console.log('buf[2] char:', String.fromCharCode(buf[2])); // "¿" 
    console.log('buf[3]:', buf[3]); // 123 
    console.log('buf[3] char:', String.fromCharCode(buf[3])); // "{" 

    // Note: The first three chars are a "Byte Order Marker" i.e. `BOM`, `ZERO WIDTH NO-BREAK SPACE`, `11101111 10111011 10111111` 

    response = JSON.parse(response); // Throws error: 'Unrecoverable exception. Unexpected token SyntaxError: Unexpected token' 
    console.log(response); 
    }); 
}); 
req.on('error', function (error) { 
    console.log(JSON.stringify(error)); 
}); 

req.on('socket', function(socket) { 
    socket.on('secureConnect', function() { 
    req.write(requestString); 
    req.end(); 
    }); 
}); 

如果我打電話trim()的響應,它的工作原理:

response = JSON.parse(response.trim()); 

或更換BOM

response = response.replace(/^\uFEFF/, ''); 
response = JSON.parse(response); 
+0

請參閱:https://community.developer.authorize.net/t5/Integration-and-Testing/json-decode -return-NULL/mp/53696#M28790 – bendiy

+0

參見:https://community.developer.authorize.net/t5/Integration-and-Testing/JSON-issues/mp/48851/highlight/true#M24533 – bendiy