如何使用PHP将其解析为数组?

时间:2016-03-22 21:40:41

标签: php app-store

Apple的应用程序商店将此对象作为收据发回。我想将它解析成一个数组。最简单的方法是什么?

{
    "signature" = "AlHSJDRPuOhUBrIarX84l/YeP+ZpxYyd592/oyF7MkVYb/s13L2W8ag2C+PG0wSHPthbv9dNMBsG4TGg3aP7r7N+oa9Rsp9C3Qu03Z9b7PRzYM2KGXHDmOMk7vDpeMVlLDPSGYz1+U3sDxJzebSpbaJmT7imzUKfggEY7xxf4czfH0yj5wNzSGTOvQ==";
    "purchase-info" = "ewoJIm9yaWdpbmFsLXB1cmNoYXNlLWRhdGUtcHN0IiA9ICIyMDE2LTAzLTIyIDEwOjUxOjAzIEFtZXJpY2EvTG9zX0Fucm9wYXRoLmZpcnN0IjsKCSJwdXJjaGFzZS1kYXRlLXBzdCIgP0wMy0yMiAxMDo1MTowMSBBbWVyaWNhL0xvc19BbmdlbGVzIjsKCSJxdWFudGl0eSIgPSAiMSI7Cn0=";
    "environment" = "Sandbox";
    "pod" = "100";
    "signing-status" = "0";
}

请注意,它不是JSON,并且json_decode无效。

1 个答案:

答案 0 :(得分:0)

这是另一个快速示例,对于那些不习惯正则表达式的人来说可能更容易阅读:

$response = '{
"signature" = "AlHSJDRPuOhUBrIarX84l/YeP+ZpxYyd592/oyF7MkVYb/s13L2W8ag2C+PG0wSHPthbv9dNMBsG4TGg3aP7r7N+oa9Rsp9C3Qu03Z9b7PRzYM2KGXHDmOMk7vDpeMVlLDPSGYz1+U3sDxJzebSpbaJmT7imzUKfggEY7xxf4czfH0yj5wNzSGTOvQ==";
"purchase-info" = "ewoJIm9yaWdpbmFsLXB1cmNoYXNlLWRhdGUtcHN0IiA9ICIyMDE2LTAzLTIyIDEwOjUxOjAzIEFtZXJpY2EvTG9zX0Fucm9wYXRoLmZpcnN0IjsKCSJwdXJjaGFzZS1kYXRlLXBzdCIgP0wMy0yMiAxMDo1MTowMSBBbWVyaWNhL0xvc19BbmdlbGVzIjsKCSJxdWFudGl0eSIgPSAiMSI7Cn0=";
"environment" = "Sandbox";
"pod" = "100";
"signing-status" = "0";
}';

$response = trim(str_replace(array('{', '}', '"', '    ', ';'), '', $response));
$responseLines = explode("\n", $response);

$result = array();
foreach ($responseLines as $responseLine) {
    $lineElements = explode(" = ", $responseLine);
    $result[$lineElements[0]] = $lineElements[1];
}

var_dump($result);