2016-02-04 39 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