本文整理汇总了PHP中Json::decode方法的典型用法代码示例。如果您正苦于以下问题:PHP Json::decode方法的具体用法?PHP Json::decode怎么用?PHP Json::decode使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类Json
的用法示例。
在下文中一共展示了Json::decode方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的PHP代码示例。
示例1: load
public static function load($path)
{
if (!is_readable($path)) {
return [];
}
$json = file_get_contents($path);
return Json::decode($json);
}
示例2: doDefault
/**
* 显示登录页(默认Action)
*/
function doDefault()
{
$data = array('a', 'b' => 'roast');
$json = new Json();
$str_encoded = $json->encode($data);
var_dump($str_encoded);
var_dump($json->decode($str_encoded));
}
示例3: loadMessagesFromFile
/**
* Loads the message translation for the specified language and category.
* @param string $messageFile string The path to message file.
* @return string[] The message array, or an empty array if the file is not found or invalid.
*/
protected function loadMessagesFromFile($messageFile) : array
{
if (!is_file($messageFile)) {
return [];
}
$messages = Json::decode(@file_get_contents($messageFile));
return is_array($messages) ? $messages : [];
}
示例4: testDecode_WithError
/**
* Tests the <code>decode()</code> method with an exception.
*
* @covers Braincrafted\Json\Json::decode()
* @covers Braincrafted\Json\Json::getError()
*/
public function testDecode_WithError()
{
try {
Json::decode('{"var1":"foo","var2":42');
$this->assertTrue(false);
} catch (JsonDecodeException $e) {
$this->assertTrue(true);
}
}
示例5: getCampaigns
/**
*
* @return CompanyEntity
*/
public function getCampaigns()
{
$request = Request::get("{$this->getCompanyId()}/campaigns");
$response = $this->getConnector()->sendRequest($request);
$data = Json::decode($response->getContent());
$json = new stdClass();
$json->campaigns = $data;
return new CampaignsEntity($json);
}
示例6: getUnsubscribersByEmail
/**
*
* @param string $email
* @return UnsubscribersEntity
*/
public function getUnsubscribersByEmail($email)
{
$request = Request::get("{$this->getCompanyId()}/unsubscribers/{$email}");
$response = $this->getConnector()->sendRequest($request);
$data = Json::decode($response->getContent());
$json = new stdClass();
$json->unsubscribers = $data;
return new UnsubscribersEntity($json);
}
示例7: parseFunction
/**
* Parses custom function calls for geting function name and parameters.
*
* @param string $function
*
* @return array
*/
private function parseFunction($function)
{
if (!Str::contains($function, ':')) {
return [$function, []];
}
list($function, $params) = explode(':', $function, 2);
$params = (array) Json::decode($params, true);
return [$function, $params];
}
示例8: testEncodeAndDecode
public function testEncodeAndDecode()
{
$value = 1;
$this->assertEquals($value, Json::decode(Json::encode($value)));
$value = 'foo';
$this->assertEquals($value, Json::decode(Json::encode($value)));
$value = ['foo' => 'bar'];
$this->assertEquals($value, Json::decode(Json::encode($value)));
$value = ['foo', 'bar'];
$this->assertEquals($value, Json::decode(Json::encode($value)));
}
示例9: update
public function update()
{
$data = Json::decode(Request::post('data'));
$id = Request::post('id', true, Validator::INT);
if (!$data) {
throw new ValidatorException('Некорректный параметр data');
}
//проверка данных
$this->validateDataForSave($data);
echo $this->_update($data, $id);
}
示例10: parseArguments
/**
* Parses the arguments of a parametrized helper.
* Arguments can be specified as a single value, or as a string in JSON format.
* @param string $text The section content specifying the helper arguments.
* @param string $defaultArgument The name of the default argument. This is used when the section content provides a plain string instead of a JSON object.
* @param array $defaultValues The default values of arguments. These are used when the section content does not specify all arguments.
* @return array The parsed arguments as an associative array.
*/
protected function parseArguments(string $text, string $defaultArgument, array $defaultValues = []) : array
{
try {
if (is_array($json = Json::decode($text))) {
return ArrayHelper::merge($defaultValues, $json);
}
throw new InvalidParamException();
} catch (InvalidParamException $e) {
$defaultValues[$defaultArgument] = $text;
return $defaultValues;
}
}
示例11: index
/**
* ControllerStyleTemplate::index()
*
* @see Load
* @see Document
* @see Language
* @see getList
* @return void
*/
public function index()
{
$this->load->auto('json');
$response = Json::decode($this->fetchUrl('http://www.necotienda.org/api/index.php?r=style/template/get'));
if ($response['response_code'] === 200) {
$this->data['templates'] = $response['data']['data'];
}
$this->template = 'style/template_list.tpl';
$this->children[] = 'common/header';
$this->children[] = 'common/nav';
$this->children[] = 'common/footer';
$this->response->setOutput($this->render(true), $this->config->get('config_compression'));
}
示例12: add
public function add()
{
$data = Json::decode(Request::post('data'));
if (empty($data['type'])) {
throw new ValidatorException('Параметр "type" не передан или пустой');
}
$real_url = eval($this->db->getOne('SELECT `url_maker` FROM ##items_types WHERE `id`=?i LIMIT 1', [$data['type']])) . '.html';
$data['params'] = Json::encode($data['item_params']);
unset($data['item_params']);
$id = parent::_add($data);
$this->db->query("INSERT INTO ##url_redirects (`old_url`,`new_url`,`type`,`system`,`comment`) VALUES (?s,?s,'I',1,?i)", [$data['item_url'], $real_url, $id]);
echo $id;
}
示例13: parse
/**
* @param string $json
*
* @throws Exception\JsonParseException
*
* @return Request|Request[]
*/
public function parse($json)
{
// decode the string
$request = Json::decode($json, $this->jsonDecodeDepthLimit);
// create a new collection
if (is_array($request) && count($request) > 0) {
// non-empty arrays are attempts at batch requests
$collection = array();
foreach ($request as $singleRequest) {
$collection[] = $this->createFrom($singleRequest);
}
return $collection;
} else {
// all other valid json is treated as a single request
return $this->createFrom($request);
}
}
示例14: unserialize
/**
* @param mixed $value
* @param bool $throwException
* @throws SerializeException
* @return mixed
*/
public static function unserialize($value, $throwException = true)
{
if ($throwException === false) {
if (!is_string($value)) {
return $value;
}
}
if (static::is($value)) {
return unserialize($value);
} elseif (Json::is($value)) {
return Json::decode($value);
}
if ($throwException == true) {
throw new SerializeException(SerializeException::NOT_SERIALIZE);
}
return $value;
}
示例15: geocode
static function geocode($place, $country = '')
{
$text = $country ? $place . ',' . $country : $place;
$temp = TempStore::getInstance();
$key = 'YahooQueryClient/geocode//' . $text;
$data = $temp->get($key);
if ($data) {
return unserialize($data);
}
$q = urlencode('select * from geo.places where text="' . $text . '"');
$url = 'http://query.yahooapis.com/v1/public/yql?q=' . $q . '&format=json';
$x = Json::decode($url);
//XXX: instead return all results as array of YahooGeocodeResult objects?
if ($x->query->count > 1) {
$item = $x->query->results->place[0];
} else {
$item = $x->query->results->place;
}
$res = new YahooGeocodeResult();
$res->name = $item->name;
$res->country = $item->country->code;
/* XXX TODO: parse admin1, admin2:
admin1: {
* code: ""
* type: "County"
* content: "Jamtland"
}
admin2: {
* code: ""
* type: "Municipality"
* content: "Härjedalen"
}
*/
$res->woeid = $item->woeid;
$res->area = new \StdClass();
$res->area->center = new YahooQueryCoordinate($item->centroid->latitude, $item->centroid->longitude);
$res->area->sw = new YahooQueryCoordinate($item->boundingBox->southWest->latitude, $item->boundingBox->southWest->longitude);
$res->area->ne = new YahooQueryCoordinate($item->boundingBox->northEast->latitude, $item->boundingBox->northEast->longitude);
//XXX this is a ugly hack until yahoo returns timezone with their response
$geoname = GeonamesClient::reverse($item->centroid->latitude, $item->centroid->longitude);
$res->timezone = $geoname->timezone;
$temp->set($key, serialize($res));
return $res;
}