简体   繁体   中英

PHP decode data JSON/SERIALIZED

I cant get this into an array..

I should decode this with php:

({serverTimestamp: "Wed, 28 Mar 2012 11:40:26 +0000",users: [{userId: "username",statuscode:0,floatingPL:-1000.00,balance:50000,equity:40000,freeMargin:4000,marginInUse:30.11,currency:"EUR",closedPL:5692.85,creditFacility:0,mostRecentUpload:"Wed, 28 Mar 2012 11:39:58 +0000"}]})

It looks like it's serialized, but unserialize() gives false in var_dump(). Json_decode gives the same..

Thank you

Example: http://codepad.org/tK4zIJj1

serialize() vs unserialize() are PHP's own data from/to string conversion functions, which can deal with circular references, classes, etc. but usually not compatible with the outer world.

  1. You should kick the ass of the provider of the "JSON" data to fix it.
  2. If ass-kicking didn't help, you can use this quickly put-together function:

     function fix_json( $j ){ $j = trim( $j ); $j = ltrim( $j, '(' ); $j = rtrim( $j, ')' ); $a = preg_split('#(?<!\\\\\\\\)\\"#', $j ); for( $i=0; $i < count( $a ); $i+=2 ){ $s = $a[$i]; $s = preg_replace('#([^\\s\\[\\]\\{\\}\\:\\,]+):#', '"\\1":', $s ); $a[$i] = $s; } //var_dump($a); $j = implode( '"', $a ); //var_dump( $j ); return $j; } 

    Example: http://codepad.org/9MpZVWrF

Yes, ghoti is right. In order for PHP to convert to JSON, all property names must be quoted in double quotes. In this case, this is the correct JSON:

({"serverTimestamp": "Wed, 28 Mar 2012 11:40:26 +0000","users": [{"userId": "username","statuscode":0,"floatingPL":-1000.00,"balance":50000,"equity":40000,"freeMargin":4000,"marginInUse":30.11,"currency":"EUR","closedPL":5692.85,"creditFacility":0,"mostRecentUpload":"Wed, 28 Mar 2012 11:39:58 +0000"}]})

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM