-
PHP
(
https://forum.zone-game.info/forumdisplay.php?f=125)
- -
Json класс
(
https://forum.zone-game.info/showthread.php?t=24912)
Json класс
вот сделал
может кому понадобится
Свернуть ↑
PHP код:
class Json{ const ARRAY_TYPE = 0; const OBJECT_TYPE = 1; private $_array = array (); private $_type = OBJECT_TYPE; public function __construct($type = self::OBJECT_TYPE) { $this->_type = $type; } public function add($value) { $this->_array[] = $value; $this->_type = self::ARRAY_TYPE; } public function get($i) { return $this->_array[$i]; }
public function toArray() { $object = new ArrayObject($this->_array); $it = $object->getIterator(); $str .= '['; while($it->valid()) { $value = $it->current(); $it->next(); if(is_int($value) || $value instanceof Json || is_float($value)) $str .= $value; else $str .= "\"$value\""; if($it->valid()) $str .= ','; } $str .= ']'; return $str; } public function toObject() { $object = $this; $str .= '{'; $str .= toJsonString($this); $str .= '}'; return $str; } public function __toString() { switch($this->_type) { case self::ARRAY_TYPE: return $this->toArray(); case self::OBJECT_TYPE: return $this->toObject(); } } }
function toJsonString($json) { $str = ''; foreach($json as $key => $value) { $str .= "\"$key\""; $str .= ':'; if(is_int($value) || $value instanceof Json || is_float($value)) $str .= $value; else $str .= "\"$value\""; $str .= ','; } $str = substr($str, 0, strlen($str) - 1); return $str; }
Свернуть ↑Развернуть ↓
Свернуть ↑
PHP код:
$array = new Json;
$array->add(0);
$array->add(1);
$array->add(2);
$array->add('test');
echo $array;
$object = new Json;
$object->test = 1;
$object->test = 'test dasd asdasd';
$object->array = new Json;
$object->array->add(0);
$object->array->add(1);
$object->array->add(2);
$object->object = new Json;
$object->object->key = 'value';
echo $object;
Свернуть ↑Развернуть ↓
исправил аля стандарт
|
Re: Json класс
PHP код:
<?php
$obj = new stdclass; $obj->test = "test dasd asdasd"; $obj->array = array(0,1,2); $obj->object = new stdclass; $obj->object->key="value"
echo json_encode($obj);
?>
|
Текущее время: 11:31. Часовой пояс GMT +3. |
|
Powered by vBulletin® Version 3.8.6
Copyright ©2000 - 2021, Jelsoft Enterprises Ltd. Перевод: zCarot