TrialPay在回调时发送格式错误的JSON

时间:2012-05-10 18:27:16

标签: php facebook json

我正在尝试在Facebook应用上实施TrialPay / Offerwall / Dealspot。在他们的documentation中,他们举例说明了他们发送给你的JSON:

{"order_id":9006316682257,"buyer":409697,"app":107032282669135,"receiver":409697,
"amount":1,"time_placed":1322622026,"update_time":1322622027,"data":"",
"items":[{"item_id":"0","title":"3 Fred Currency","description":"Make it rain!",
"image_url":"http:\/\/external.ak.fbcdn.net\/safe_image.php?d=AQDldsPcWsejAJdC&url=http\u00253A\u00252F\u00252Fwww.etftrends.com\u00252Fwp-content\u00252Fuploads\u00252F2011\u00252F10\u00252Fcurrency-trading.jpg",
"product_url":"","price":1,"data":"{\"modified\":{\"product\":\"URL_TO_APP_CURR_WEBPAGE\",
\"product_title\":\"Fred Currency\",\"product_amount\":3,\"credits_amount\":1}}"}],"status":"placed"}

他们说如果你把它作为一个数组json_decode,你应该得到这个:

Array (
  [order_id] => 9006316682257
  [buyer] => 409697
  [app] => 107032282669135
  [receiver] => 409697
  [amount] => 1
  [time_placed] => 1322622026
  [update_time] => 1322622027
  [data] =>
  [items] => Array (
               [0] => Array (
                        [item_id] => 0
                        [title] => 3 Fred Currency
                        [description] => Make it rain!
                        [image_url] => http://external.ak.fbcdn.net/safe_image.php?d=AQDldsPcWsejAJdC&url=http%3A%2F%2Fwww.etftrends.com%2Fwp-content%2Fuploads%2F2011%2F10%2Fcurrency-trading.jpg
                        [product_url] =>
                        [price] => 1
                        [data] => {"modified":{"product":"URL_TO_APP_CURR_WEBPAGE","product_title":"Fred Currency","product_amount":3,"credits_amount":1}}
                      )
             )
  [status] => placed
)

但事实并非如此,数据实际上是这样的:

[data] => "{"modified":{"product":"URL_TO_APP_CURR_WEBPAGE","product_title":"Fred Currency","product_amount":3,"credits_amount":1}}"

字符串中的JSON导致它是无效的JSON。是否有一种直接的方法来删除这些引号?

3 个答案:

答案 0 :(得分:2)

首先,看起来您需要在Trialpay的网站上完成配置,因此URL_TO_APP_CURR_WEBPAGE。这里的问题可能是您尚未完成应用程序配置,以达到生成有效JSON所需的程度。

如果那不是答案,如果你仍然得到无效的JSON(我同意,那是无效的)我建议联系你的Trialpay代表。他们通常非常敏感,我们在游戏开发过程中确实发现了一些问题。

祝你好运 - 如果/当你找到更多信息时,请回到这里。

干杯

答案 1 :(得分:0)

您是否尝试将json_decode($json_string, true);转换为关联数组。

答案 2 :(得分:0)

开发人员TrialPay。我们的文档网站可能会有拼写错误,我会发送一个附注来仔细检查。

与此同时,我已经验证了Facebook在完成基于商品的应用内货币订单时传递给服务器端回调的实际JSON应该是有效的,并且正确解码到上面所需的结果

如果您遇到此线程范围之外的任何其他问题,请随时直接ping我。

编辑:

复制代码并对JSONLint进行验证后,我在您提到的那一刻就遇到了问题。但是,在\"product_title\"之前删除了坏的换行符后,我能够正确验证。下面包含示例PHP代码段:

<?php

$order_details = '{"order_id":9006316682257,"buyer":409697,"app":107032282669135,"receiver":409697,"amount":1,"time_placed":1322622026,"update_time":1322622027,"data":"","items":[{"item_id":"0","title":"3 Fred Currency","description":"Make it rain!","image_url":"http:\/\/external.ak.fbcdn.net\/safe_image.php?d=AQDldsPcWsejAJdC&url=http\u00253A\u00252F\u00252Fwww.etftrends.com\u00252Fwp-content\u00252Fuploads\u00252F2011\u00252F10\u00252Fcurrency-trading.jpg","product_url":"","price":1,"data":"{\"modified\":{\"product\":\"URL_TO_APP_CURR_WEBPAGE\",\"product_title\":\"Fred Currency\",\"product_amount\":3,\"credits_amount\":1}}"}],"status":"placed"}';
$order_details_decoded = json_decode($order_details, true);
$order_details_decoded['items'][0]['data'] = json_decode($order_details_decoded['items'][0]['data'], true);
print_r($order_details_decoded);

正如我早些时候提到的,如果有任何其他问题超出了本主题的范围,请随时直接ping我。