本文整理汇总了PHP中Json类的典型用法代码示例。如果您正苦于以下问题:PHP Json类的具体用法?PHP Json怎么用?PHP Json使用的例子?那么, 这里精选的类代码示例或许可以为您提供帮助。
在下文中一共展示了Json类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的PHP代码示例。
示例1: parseMustReturnArray
/**
* @test
*/
public function parseMustReturnArray()
{
$expected = ['some' => 1, 'valid' => false, 'payloads' => [1, "10", 1.1, null, true]];
$json = new Json();
$parsed = $json->parse(json_encode($expected));
$this->assertEquals($expected, $parsed);
}
示例2: gerateBilling
public function gerateBilling()
{
$order = null;
$customers = $this->getUserID();
$structJsonArray = array();
foreach ($customers as $customer) {
$structJson = new Json();
$structJson->setCustomerId($customer['zoho_books_contact_id']);
$structJson->setDate(date('d-m-Y'));
$invoices_per_costumer = $this->getUserInvoice($customer['zoho_books_contact_id']);
//var_dump($invoices_per_costumer);
foreach ($invoices_per_costumer as $invoice_each) {
$item = new Item();
$order++;
$item->setItemId("460000000027017");
$item->setProjectId("");
$item->setExpenseId("");
$item->setName("Print Services");
$item->setDescription($invoice_each['description']);
$item->setItemOrder($order);
$item->setRate($invoice_each['rate']);
$item->setUnit("Nos");
$item->setQuantity(1.0);
$item->setDiscount(0.0);
$item->setTaxId("460000000027005");
$structJson->setLineItems($item);
$item = null;
}
$structJson->setNotes("Thanks for your business.");
$order = null;
$structJsonArray[] = $structJson;
$structJson = null;
}
return $structJsonArray;
}
示例3: setJsonResponse
public function setJsonResponse(Json $json)
{
if ($this->error) {
$json->setError($this->error, $this->statusCode);
}
$this->response = $json->__toString();
}
示例4: 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));
}
示例5: test_parse_returnsSelf_ifOutputIsValid
/**
* parse() should return true if $output is valid json
*/
public function test_parse_returnsSelf_ifOutputIsValid()
{
$output = '{"foo":"bar"}';
$response = new Json();
$this->assertTrue($response->parse($output));
$this->assertEquals(['foo' => 'bar'], $response->getData());
return;
}
示例6: serialize
/**
* Serialize array to JSON string.
*
* @param array $args
* @return string
* @throws GraphCommons\Util\JsonException
*/
public function serialize(...$args) : string
{
$json = new Json($this->unserialize());
if ($json->hasError()) {
$jsonError = $json->getError();
throw new JsonException(sprintf('JSON error: code(%d) message(%s)', $jsonError['code'], $jsonError['message']), $jsonError['code']);
}
return (string) $json->encode($args);
}
示例7: testWrite
/**
* Tests Json::write
*/
public function testWrite()
{
$json = new Json();
$json->setup(['target' => 'php://output']);
ob_flush();
ob_start();
$json->write(['foo' => 'bar']);
$output = ob_get_clean();
$this->assertEquals('{"foo":"bar"}', $output);
}
示例8: output
/**
* 页面内容输出
* @param boolean $fetch 是否提取输出结果
* @return string JSON结果
*/
function output($fetch = false)
{
ob_start();
$json = new Json();
echo $json->encode($this->value);
$content = ob_get_contents();
if ($fetch) {
ob_end_clean();
} else {
ob_end_flush();
}
return $content;
}
示例9: extendPostRequest
public function extendPostRequest($aMerchantNo)
{
try {
$this->iLogWriter = new LogWriter();
$this->iLogWriter->logNewLine("TrustPayClient V3.0.0 交易开始==========================");
MerchantConfig::getLogWriterObject($this->iLogWriter);
//0、检查传入参数是否合法
if ($aMerchantNo <= 0 || $aMerchantNo > MerchantConfig::getMerchantNum()) {
throw new TrxException(TrxException::TRX_EXC_CODE_1008, TrxException::TRX_EXC_MSG_1008, '配置文件中商户数为' . MerchantConfig::getMerchantNum() . ", 但是请求指定的商户配置编号为{$aMerchantNo} !");
}
//1、检查交易请求是否合法
$this->iLogWriter->logNewLine('检查交易请求是否合法:');
$this->checkRequest();
$this->iLogWriter->log('正确');
//2、取得交易报文
$tRequestMessage = $this->getRequestMessage();
//3、组成完整交易报文
$this->iLogWriter->log("完整交易报文:");
$tRequestMessage = $this->composeRequestMessage($aMerchantNo, $tRequestMessage);
$this->iLogWriter->log($tRequestMessage);
//4、对交易报文进行签名
$tRequestMessage = MerchantConfig::signMessage($aMerchantNo, $tRequestMessage);
//5、发送交易报文至网上支付平台
$tResponseMessage = $this->sendMessage($tRequestMessage);
//6、验证网上支付平台响应报文的签名
$this->iLogWriter->logNewLine('验证网上支付平台响应报文的签名:');
MerchantConfig::verifySign($tResponseMessage);
$this->iLogWriter->log('正确');
//7、生成交易响应对象
$this->iLogWriter->logNewLine('生成交易响应对象:');
$this->iLogWriter->logNewLine('交易结果:[' . $tResponseMessage->getReturnCode() . ']');
$this->iLogWriter->logNewLine('错误信息:[' . $tResponseMessage->getErrorMessage() . ']');
} catch (TrxException $e) {
$tResponseMessage = new Json();
$tResponseMessage->initWithCodeMsg($e->getCode(), $e->getMessage() . " - " . $e->getDetailMessage());
if ($this->iLogWriter != null) {
$this->iLogWriter->logNewLine('错误代码:[' + $tResponseMessage->getReturnCode() . '] 错误信息:[' . $tResponseMessage->getErrorMessage() . ']');
}
} catch (Exception $e) {
$tResponseMessage = new Json();
$tResponseMessage->initWithCodeMsg(TrxException::TRX_EXC_CODE_1999, TrxException::TRX_EXC_MSG_1999 . ' - ' . $e->getMessage());
if ($this->iLogWriter != null) {
$this->iLogWriter->logNewLine('错误代码:[' . $tResponseMessage->getReturnCode() . '] 错误信息:[' . $tResponseMessage->getErrorMessage() . ']');
}
}
if ($this->iLogWriter != null) {
$this->iLogWriter->logNewLine("交易结束==================================================\n\n\n\n");
$this->iLogWriter->closeWriter(MerchantConfig::getTrxLogFile());
}
return $tResponseMessage;
}
示例10: shipping
public function shipping()
{
$json = array();
$this->load->library('user');
if ($this->user->isLogged()) {
$this->language->load('checkout/checkout');
$this->tax->setZone($shipping_address['country_id'], $shipping_address['zone_id']);
if (!isset($this->session->data['shipping_methods'])) {
$quote_data = array();
$this->load->model('setting/extension');
$results = $this->model_setting_extension->getExtensions('shipping');
foreach ($results as $result) {
if ($this->config->get($result['code'] . '_status')) {
$this->load->model('shipping/' . $result['code']);
$quote = $this->{'model_shipping_' . $result['code']}->getQuote($shipping_address);
if ($quote) {
$quote_data[$result['code']] = array('title' => $quote['title'], 'quote' => $quote['quote'], 'sort_order' => $quote['sort_order'], 'error' => $quote['error']);
}
}
}
$sort_order = array();
foreach ($quote_data as $key => $value) {
$sort_order[$key] = $value['sort_order'];
}
array_multisort($sort_order, SORT_ASC, $quote_data);
$this->session->data['shipping_methods'] = $quote_data;
}
}
$this->load->library('json');
$this->response->setOutput(Json::encode($json));
}
示例11: render
function render()
{
if (!$this->name) {
throw new \Exception('must set a name');
}
if (!$this->xhr_url) {
throw new \Exception('must set xhr url');
}
if (!$this->js_format_result || !$this->result_fields) {
throw new \Exception('need js code');
}
$header = XhtmlHeader::getInstance();
$header->includeCss('core_dev/js/ext/yui/2.9.0/build/fonts/fonts-min.css');
$header->includeCss('core_dev/js/ext/yui/2.9.0/build/autocomplete/assets/skins/sam/autocomplete.css');
$header->includeCss('core_dev/js/ext/yui/2.9.0/build/button/assets/skins/sam/button.css');
$header->includeJs('core_dev/js/ext/yui/2.9.0/build/yahoo-dom-event/yahoo-dom-event.js');
$header->includeJs('core_dev/js/ext/yui/2.9.0/build/get/get-min.js');
$header->includeJs('core_dev/js/ext/yui/2.9.0/build/animation/animation-min.js');
$header->includeJs('core_dev/js/ext/yui/2.9.0/build/datasource/datasource-min.js');
$header->includeJs('core_dev/js/ext/yui/2.9.0/build/autocomplete/autocomplete-min.js');
$header->includeJs('core_dev/js/ext/yui/2.9.0/build/element/element-min.js');
$header->includeJs('core_dev/js/ext/yui/2.9.0/build/button/button-min.js');
$div_holder = 'yui_ac' . mt_rand();
$container_holder = 'ac_contain_' . mt_rand();
$button_id = 'ac_toggle_' . mt_rand();
$input_id = 'ac_input_' . mt_rand();
$header->embedJs('function highlight(s,h)' . '{' . 'var regex = new RegExp("("+h+")","ig");' . 'return s.replace(regex, "<span class=\\"highlighted\\">$1</span>");' . '}');
$header->embedCss('label {' . 'color:#E76300;' . 'font-weight:bold;' . '}' . '#' . $div_holder . ' {' . 'width:20em;' . '}' . '.yui-ac .result {position:relative;height:20px;}' . '.yui-ac .name {position:absolute;bottom:0;}' . '.highlighted {color:#CA485E;font-weight:bold; }' . '.yui-ac .yui-button {vertical-align:middle;}' . '.yui-ac .yui-button button {background: url(http://developer.yahoo.com/yui/examples/autocomplete/assets/img/ac-arrow-rt.png) center center no-repeat}' . '.yui-ac .open .yui-button button {background: url(http://developer.yahoo.com/yui/examples/autocomplete/assets/img/ac-arrow-dn.png) center center no-repeat}' . '.yui-skin-sam .yui-ac-input {position:static; vertical-align:middle;}' . '.yui-skin-sam .yui-ac-content {' . 'max-height:250px;overflow:auto;overflow-x:hidden;' . '}');
$res = 'YAHOO.example.CustomFormatting = (function(){' . 'var oDS = new YAHOO.util.ScriptNodeDataSource("' . $this->xhr_url . '");' . 'oDS.responseSchema = {' . 'resultsList:"records",' . 'fields:' . Json::encode($this->result_fields, false) . '};' . 'var oAC = new YAHOO.widget.AutoComplete("' . $input_id . '","' . $container_holder . '", oDS);' . 'oAC.minQueryLength = 0;' . 'oAC.queryDelay = ' . $this->query_delay . ';' . 'oAC.animSpeed = 0.01;' . 'oAC.maxResultsDisplayed = 100;' . 'oAC.forceSelection = true;' . 'oAC.generateRequest = function(sQuery) {' . 'return sQuery + "&format=json";' . '};' . 'oAC.resultTypeList = false;' . 'oAC.formatResult = function(oResultData, sQuery, sResultMatch) {' . $this->js_format_result . '};' . 'oAC.itemSelectEvent.subscribe(function(sType, aArgs) {' . 'var oData = aArgs[2];' . 'var input = document.createElement("input");' . 'input.setAttribute("type", "hidden");' . 'input.setAttribute("name", "' . $this->name . '");' . 'input.setAttribute("value", oData.id);' . 'document.getElementById("' . $div_holder . '").appendChild(input);' . '});' . 'var validateForm = function() {' . 'return true;' . '};' . 'return {' . 'oDS: oDS,' . 'oAC: oAC,' . 'validateForm: validateForm' . '}' . '})();';
$in = new XhtmlComponentInput();
$in->name = $input_id;
$in->width = 200;
// XXXX HACK, should not set width at all.. but we do it now so button dont end up on the next line
return '<div id="' . $div_holder . '">' . $in->render() . '<div id="' . $container_holder . '"></div>' . '</div>' . js_embed($res);
}
示例12: actionProduct
/**
* Lists all Barangalias models.
* @return mixed
*/
public function actionProduct()
{
$out = [];
if (isset($_POST['depdrop_parents'])) {
$parents = $_POST['depdrop_parents'];
if ($parents != null) {
$id = $parents[0];
$model = Barang::find()->asArray()->where(['PARENT' => $id])->andwhere('STATUS <> 3')->all();
// print_r($model);
// die();
//$out = self::getSubCatList($cat_id);
// the getSubCatList function will query the database based on the
// cat_id and return an array like below:
// [
// ['id'=>'<sub-cat-id-1>', 'name'=>'<sub-cat-name1>'],
// ['id'=>'<sub-cat_id_2>', 'name'=>'<sub-cat-name2>']
// ]
foreach ($model as $key => $value) {
$out[] = ['id' => $value['KD_BARANG'], 'name' => $value['NM_BARANG']];
}
echo json_encode(['output' => $out, 'selected' => '']);
return;
}
}
echo Json::encode(['output' => '', 'selected' => '']);
}
示例13: Set
public static function Set($Filename, $Data, $Options = JSON_PRETTY_PRINT)
{
$Path = "config/{$Filename}.json";
$Return = Json::Encode($Path, $Data, $Options);
self::LoadFile($Filename);
return $Return;
}
示例14: get_data_for_main_page
public function get_data_for_main_page()
{
$db = new Db();
$components = $db->getAll("SELECT `title`,`name` FROM ##extensions WHERE `type`='component' AND `enabled`=1");
$modules = $db->getAll('SELECT `name`,`id` FROM ##modules WHERE `published`=1');
echo Json::encode([$components, $modules]);
}
示例15: testGetErrorMessage
/**
* Tests getErrorMessage.
*/
public function testGetErrorMessage()
{
$errors = array(JSON_ERROR_NONE => 'No errors', JSON_ERROR_DEPTH => 'Maximum stack depth exceeded', JSON_ERROR_STATE_MISMATCH => 'Underflow or the modes mismatch', JSON_ERROR_CTRL_CHAR => 'Unexpected control character found', JSON_ERROR_SYNTAX => 'Syntax error, malformed JSON', JSON_ERROR_UTF8 => 'Malformed UTF-8 characters, possibly incorrectly encoded', 999999999 => 'Unknown error');
foreach ($errors as $no => $message) {
$this->assertSame($message, Json::getErrorMessage($no));
}
}