json string to associative array

This commit is contained in:
sc0Vu 2017-12-29 16:39:17 +08:00
parent 518a72b422
commit b61d90216e
2 changed files with 13 additions and 2 deletions

View File

@ -337,7 +337,7 @@ class Utils
/**
* jsonToArray
*
* @param stdClass|array $json
* @param stdClass|array|string $json
* @param int $depth
* @return array
*/
@ -374,8 +374,15 @@ class Utils
}
}
}
} elseif (is_string($json)) {
$json = json_decode($json, true);
if (JSON_ERROR_NONE !== json_last_error()) {
throw new InvalidArgumentException('json_decode error: ' . json_last_error_msg());
}
return $json;
} else {
throw new InvalidArgumentException('jsonToArray json must be array or stdClass.');
throw new InvalidArgumentException('The json param to jsonToArray must be array or stdClass or string.');
}
return $json;
}

View File

@ -366,6 +366,10 @@ class UtilsTest extends TestCase
$jsonArrayDepth2 = Utils::jsonToArray($jsonArrayDepth1, 2);
$this->assertEquals($jsonArrayDepth2, $jsonAssoc);
$jsonArray = Utils::jsonToArray($this->testJsonMethodString);
$this->assertEquals($jsonArray, $jsonAssoc);
}
/**