Konversi JSON – XML – Array dengan PHP

Kamis, 2 Februari 2012 | 07:00

Tutorial kali ini akan membahas konversi antar 3 data interchange yang populer, yaitu JSON, XML dan Array. Untuk library konversi ini, gunakan salah satu library buatan om Rochak Chauchan dari India yang dipublish pada PHPClasses.org.

Untuk file librarynya, kurang lebih sebagai berikut:

errorLog); 
        if($c>0){ 
            $i=$c-1; 
            $return="
".$this->errorLog[$i]."
"; } echo $return; } /** * Function to display complete error log for debugging purpose * * @access public * @return string */ public function displayErrorLog(){ $return="No errors were encountered."; $c=count($this->errorLog); if($c>0){ $return=""; for($i=0;$i<$c;$i++){ $return.="
".$this->errorLog[$i]."
"; } } echo $return; } /** * Function to recursivly parse Xml Content * * @param mixed $ret * @access private * @return array on success and false on failure */ private function parseXml($ret) { $return=false; if(is_object($ret)){ $ret=(array)$ret; $this->parseXml($ret); } if(is_array($ret)){ foreach($ret as $k=>$v){ if(is_object($v)){ $return[$k]=$this->parseXml($v); } else { $return[$k]=$v ; } } } return $return; } /** * Function to convert XML into Array * * @param string $xmlContent * @access public * @return array on success and false on failure */ public function convertXmlToArray($xmlContent){ $return=false; $ret=simplexml_load_string($xmlContent); if($ret===false){ $this->errorLog[]="Invalid XML content: $xmlContent in function: ".__FUNCTION__." on line: ".__LINE__." in filename= ".__FILE__; return false; } else{ $return=$this->parseXml($ret); if($return===false){ $this->errorLog[]="Failed to parse XML content in function: ".__FUNCTION__." on line: ".__LINE__." in filename= ".__FILE__; return false; } } return $return; } /** * Function to recursivly parse Array Content * * @param mixed $ret * @access private * @return string(xml) on success and false on failure */ private function parseArray($array) { if(is_array($array)){ foreach($array as $k=>$v){ if(trim($k)==""){ $this->errorLog[]="Array needs to be associative as parameter in function: ".__FUNCTION__." on line: ".__LINE__." in filename= ".__FILE__; return false; } else{ if (is_numeric($k)) { $k="nodeValue$k"; } if(is_array($v)){ $return.="<$k>".$this->parseArray($v).""; } else { $return.="<$k>$v"; } } } } else{ $this->errorLog[]="Invalid array in function: ".__FUNCTION__." on line: ".__LINE__." in filename= ".__FILE__; return false; } return $return; } /** * Function to convert an associative array into XML * * @param string $array * @access public * @return string(xml) on success and false on failure */ public function convertArrayToXML($array){ $return=""; $return.=$this->parseArray($array); $return.=""; return $return; } /** * Function to convert an JSON into XML * * @param string $json * @access public * @return string(xml) on success and false on failure */ public function convertJsonToXML($json){ if(!is_string($json)){ $this->errorLog[]="The first parameter should to be string in function: ".__FUNCTION__." on line: ".__LINE__." in filename= ".__FILE__; return false; } $array=json_decode($json,true); if($array===false){ $this->errorLog[]="Failed to decode JSON in function: ".__FUNCTION__." on line: ".__LINE__." in filename= ".__FILE__; return false; } else{ return $this->convertArrayToXML($array); } } /** * Function to convert an JSON into array * * @param string $json * @access public * @return array on success and false on failure */ public function convertJsonToArray($json){ if(!is_string($json)){ $this->errorLog[]="The first parameter should to be string in function: ".__FUNCTION__." on line: ".__LINE__." in filename= ".__FILE__; return false; } $array=json_decode($json,true); if($array===false){ $this->errorLog[]="Failed to decode JSON in function: ".__FUNCTION__." on line: ".__LINE__." in filename= ".__FILE__; return false; } else{ return $array; } } /** * Function to parse String and convert it into array * * @param array $array * @access public * @return array on success and false on failure * @todo refactor the code from line 205-222 (automate it) */ public function convertStringToArray($string,&$myarray=""){ $lines = explode(LINE_SEPERATOR,$string); foreach ($lines as $value){ $items = explode(NODE_SEPERATOR,$value); if (sizeof($items) == 2){ $myarray[$items[0]] = $items[1]; } elseif (sizeof($items) == 3){ $myarray[$items[0]][$items[1]] = $items[2]; } elseif (sizeof($items) == 4){ $myarray[$items[0]][$items[1]] [$items[2]] = $items[3]; } elseif (sizeof($items) == 5){ $myarray[$items[0]][$items[1]] [$items[2]][$items[3]] = $items[4]; } elseif (sizeof($items) == 6){ $myarray[$items[0]][$items[1]] [$items[2]][$items[3]][$items[4]] = $items[5]; } elseif (sizeof($items) == 7){ $myarray[$items[0]][$items[1]] [$items[2]][$items[3]][$items[4]][$items[5]] = $items[6]; } } return $myarray; } /** * Function to parse Array and convert it into string * * @param array $array * @access private * @return string on success and false on failure */ private function convertArrayToString($myarray,&$output="",&$parentkey=""){ if(is_array($myarray)){ if (trim($parentkey)=="") { $parentkey=LINE_SEPERATOR; } foreach($myarray as $key=>$value){ if (is_array($value)) { $parentkey .= $key.NODE_SEPERATOR; $this->convertArrayToString($value,$output,$parentkey); $parentkey = ""; } else { $output .= $parentkey.$key.NODE_SEPERATOR.$value.LINE_SEPERATOR; } } } else{ $this->errorLog[]="Invalid array in function: ".__FUNCTION__." on line: ".__LINE__." in filename= ".__FILE__; return false; } return $output; } /** * Function to convert XML into string * * @param string $xml * @return string on success and false on failure */ public function convertXmltoString($xml){ $array=$this->convertXmlToArray($xml); if ($array===false) { return false; } else { return $this->convertArrayToString($array); } } } ?>

Untuk contoh penerapan silakan tuju ke halaman berikutnya.

© 2014 Unit Pelaksana Teknis TIK. All rights reserved.
LOGIN | SITEMAP