Words don't say much. Just go ahead (if you have Chinese.) Please note that JSON only UTF-8 encoded)
First you need to have a JSON file data
{
"Index": {
"title": "Indexmytitle",
"keywords": "Chinese",
"Content": "Chinese",
"description": "Chinese"
},
"goods": {
& nbsp "title": "Goodsmytitle",
"keywords": "Goodskeywords", br> "content": "Goodsmycontent",
&NBS P "description": "Goodsmydes"
},
"Shop": {
"title": "Shopmytitle",
"keywords": "Shopkeywords",
"Content": "Shopmycontent",
"description": "Shopmudes",
"Description1": "Shopmudes"
}
}
And then what. You need to build a public class in CI, Appliction/librarys directory json.php
<?php if (!defined (' BasePath ')) exit (' No Direct script access allowed ');
Class Json {
Public Function some_function ($seo _category) {
$path = ' HTTP://127.0.0.1/project name/public/json.json '; File path
$json _result= file_get_contents ($path); Read the JSON file into a string.
$json _array= Json_decode ($json _result,true); Json_decode encode strings in JSON format, convert to array form
return $json _array[$seo _category]; Returns the result based on the key value
}
}
?>
If you can't see it on top. That's the equivalent of the two, but it's more troublesome. If you change it, you change the class library.
<?php if (!defined (' BasePath ')) exit (' No Direct script access allowed ');
Class Json {
Public Function some_function ($seo _category) {
$path = ' Http://127.0.0.1/ttzkq/public/json.json '; File path
$json _result= file_get_contents ($path); Read the JSON file into a string.
$json _array= Json_decode ($json _result,true); Json_decode encode strings in JSON format, convert to array form
Two-dimensional array to one-dimensional array
foreach ($json _array as $k = + $v) {
$ $k = $v;
}
if ($seo _category = = = ' Index ') {
return $index;
}elseif ($seo _category = = ' goods ') {
return $goods;
}elseif ($seo _category = = ' Shop ') {
return $shop;
}
}
}
?>
And then look inside the controller,
Public Function __construct () {
$this->load->library (' json '); Loading the JSON Data class library
}
Called in the bottom method, and passed the value to the HTML view inside
$top [' Json_result ']= $this->json->some_function (' index ');
$this->load->view ("Templates/top", $top);
Last viewed View
<meta name= ' keywords ' content= "<?php echo $json _result[' keywords '];? > ">
<meta name= ' content ' content= ' <?php echo $json _result[' content '];? > ">
<meta name= ' description ' content= "<?php echo $json _result[' description '];? > ">
Original works. For learning purposes only,
PHP gets JSON file data and dynamically modifies website header file meta information--based on CI framework