[ create a new paste ] login | about

Link: http://codepad.org/9MpZVWrF    [ raw code | output | fork ]

PHP, pasted on Mar 28:
<?php
//header('Content-type: text/plain; charset=UTF-8');

$j1 = '
({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"}]})
';

$j2 = '{ aaa: "a\"b\"c",bbb:"ab\"c", "cc\"cc":"ccc" }';

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;
}

var_dump( json_decode(fix_json($j1)));
var_dump( json_decode(fix_json($j2)));


Output:
object(stdClass)#1 (2) {
  ["serverTimestamp"]=>
  string(31) "Wed, 28 Mar 2012 11:40:26 +0000"
  ["users"]=>
  array(1) {
    [0]=>
    object(stdClass)#2 (11) {
      ["userId"]=>
      string(8) "username"
      ["statuscode"]=>
      int(0)
      ["floatingPL"]=>
      float(-1000)
      ["balance"]=>
      int(50000)
      ["equity"]=>
      int(40000)
      ["freeMargin"]=>
      int(4000)
      ["marginInUse"]=>
      float(30.11)
      ["currency"]=>
      string(3) "EUR"
      ["closedPL"]=>
      float(5692.85)
      ["creditFacility"]=>
      int(0)
      ["mostRecentUpload"]=>
      string(31) "Wed, 28 Mar 2012 11:39:58 +0000"
    }
  }
}
object(stdClass)#1 (3) {
  ["aaa"]=>
  string(5) "a"b"c"
  ["bbb"]=>
  string(4) "ab"c"
  ["cc"cc"]=>
  string(3) "ccc"
}


Create a new paste based on this one


Comments: