Kuxazululiwe: funda idatha yefayela le-json usebenzisa

Ukufunda nokukhohlisa amafayela e-JSON kuwumsebenzi ovamile emhlabeni wokuthuthukiswa kwe-PHP. I-JSON, emele i-JavaScript Object Notation, isiphenduke indinganiso eyamukelwa kabanzi yokushintshisana kwedatha ngenxa yobulula bayo nesakhiwo esinesisindo esincane. Ngaphandle kwegama layo, i-JSON iyifomethi yedatha ezimele yolimi. Lokhu kusho ukuthi singayisebenzisa ngokuphumelelayo ku-PHP kanye nezinye izilimi ezifana ne-JavaScript, C#, Python, njll. Kulo mhlahlandlela, sizobhekisisa ukuthi singayifunda kanjani idatha yefayela le-JSON sisebenzisa i-PHP ngesinyathelo-by- isinyathelo sokuhamba kwekhodi.

I-PHP ihlinzeka ngemisebenzi eyakhelwe ngaphakathi yokuphatha idatha ye-JSON, inxenxa onjiniyela ngobulula nokuhambisana phambili. Kungakhathaliseki ukuthi usebenza kuhlelo lokusebenza oluncane noma uphatha idathasethi enkulu, i-PHP ne-JSON yenza inhlanganisela enamandla.

Ukufunda Amafayela e-JSON ku-PHP

Khipha Amafayela e-JSON usebenzisa i-json_decode()

Ukuze ufunde idatha ye-JSON ku-PHP, ngokuyinhloko sicela usekelo lomsebenzi we-json_decode. Kuthatha iyunithi yezinhlamvu enekhodi ye-JSON futhi iyiguqule ibe ukuguquguquka kwe-PHP.

$jsonData = file_get_contents('data.json');
$data = json_decode($jsonData, true);

Kule khodi engenhla, siqale safunda ifayela le-JSON data.json sisebenzisa umsebenzi we-file_get_contents(). Umsebenzi json_decode() ubese uguqula iyunithi yezinhlamvu ye-JSON efakwe kuhlelo lwe-PHP noma into ngokusekelwe kupharamitha ye-boolean yesibili eyitholayo. Uma leyo pharamitha iyiqiniso, umsebenzi ubuyisela amalungu afanayo ahlangene.

Sebenza kudatha ye-JSON ku-PHP

Uma sesinedatha yethu ye-JSON ehlukaniselwe kokuguquguqukayo kwe-PHP, singayiphindaphinda kalula futhi sifinyelele izici ngazinye.

foreach($data as $key => $value) {
    echo $key . ": " . $value;
}

Kule khodi engenhla, sixhuma idatha ye-JSON ekhishiwe. Kuwo wonke amapheya enani elingukhiye, siphrinta ukhiye kanye nenani elihambisanayo.

Uhambo lwethu lokufunda ifayela le-JSON ku-PHP alugcini lapha. Ulimi lunikeza eminye imisebenzi embalwa ongayisebenzisa ukuze wenze ngokwezifiso ukusebenzisa kwakho ukuze kulingane nezidingo zakho ezithile.

Ukuphatha amaphutha nge-json_last_error_msg()

Ukuphatha iphutha kuyisici esibalulekile sokusebenza ne-JSON ku-PHP. Ngenhlanhla, i-PHP inikeza amandla aqondile okuphatha amaphutha okuphatha imisebenzi ye-JSON.

if (json_last_error() !== JSON_ERROR_NONE) {
    echo 'Error: '. json_last_error_msg();
}

Kumazwibela ekhodi angenhla, siyaqinisekisa ukuthi ukusebenza kokugcina kubangele iphutha le-JSON kusetshenziswa umsebenzi othi json_last_error(). Uma kukhona iphutha, siliphrinta sisebenzisa umsebenzi we-json_last_error_msg().

Ifaka ikhodi ye-PHP ku-JSON nge-json_encode()

I-PHP futhi isivumela ukuthi siguqule kalula okuguquguqukayo kwe-PHP sibuyele kudatha efomethwe yi-JSON ngomsebenzi we-json_encode().

$arrayData = ['key' => 'value', 'anotherKey' => 'anotherValue'];
$jsonData = json_encode($arrayData);

Esigabeni esingenhla, sithatha uhlu oluhlangene lwe-PHP futhi siyiguqule ibe yiyunithi yezinhlamvu enekhodi ye-JSON.

Kulesi sihloko, sibheke kabanzi ngokufunda nokusebenza ngedatha yefayela le-JSON ku-PHP. Imojula ye-php-json ihlinzeka ngamakhono aqinile okuphatha idatha ye-JSON elungele uhla lwezinhlelo zokusebenza. Kusukela ekubhalweni kwekhodi nokuqopha idatha, kuya ekulawuleni amaphutha ngendlela ephumelelayo, i-PHP iphusha imingcele ngokuqondisa ukusebenza ngedatha ye-JSON.

Okuthunyelwe okuhlobene:

Shiya amazwana