المثال الأول: طباعة JSON بدون تنسيق
json_encode () تُستخدم وظيفة PHP لتحليل أي بيانات JSON. قم بإنشاء ملف باسم exp1.php مع الكود التالي لقراءة بيانات JSON بسيطة وطباعة الإخراج. هنا ، يتم الإعلان عن مصفوفة ترابطية لإنشاء بيانات JSON. لا يتم تطبيق أي تنسيق لبيانات JSON في الكود. لذلك ، ستتم طباعة بيانات JSON في سطر واحد بتنسيق JSON.
exp1.php
<؟بي أتش بي
//نعلن المجموعة
دورات $= مجموعة('وحدة 1'=>'لغة البرمجة'و'الوحدة -2'=>'جافا سكريبت'و'الوحدة -3'=>'CSS3'و
'الوحدة -4'=>'بي أتش بي')؛
//اطبع المصفوفةفيتنسيق JSON بسيط
رما - طرد - قذفjson_encode(دورات $)؛
؟>
انتاج:
سيظهر الإخراج التالي بعد تنفيذ الملف من المتصفح.
http: //localhost/json/exp1.php
مثال 2: طباعة JSON باستخدام خيار JSON_PRETTY_PRINT ووظيفة header ()
PHP لديها خيار اسمه 'JSON_PRETTY_PRINT' الذي يستخدم مع json_encode () وظيفة لطباعة بيانات JSON بمحاذاة صحيحة وتنسيق معين. قم بإنشاء ملف باسم exp2.php مع الكود التالي. في الكود ، يتم استخدام نفس المصفوفة من المثال السابق لمعرفة الاستخدام JSON_PRETTY_PRINT اختيار. رأس () يتم استخدام الوظيفة هنا لإعلام المتصفح بمحتوى الملف. لن يتم تطبيق أي تنسيق بدون هذه الوظيفة.
exp2.php
<؟بي أتش بي//نعلن المجموعة
دورات $= مجموعة('وحدة 1'=>'لغة البرمجة'و'الوحدة -2'=>'جافا سكريبت'و'الوحدة -3'=>'CSS3'و
'الوحدة -4'=>'بي أتش بي')؛
//قم بإخطار المتصفح عن ملفنوعالتابعملفباستخدام الرأسوظيفة
رأس('نوع المحتوى: نص / جافا سكريبت')؛
//اطبع المصفوفةفيتنسيق JSON بسيط
رما - طرد - قذفjson_encode(دورات $، JSON_PRETTY_PRINT)؛
؟>
انتاج:
سيظهر الإخراج التالي بعد تنفيذ الملف من المتصفح. سيتم تطبيق خط معين ومحاذاة.
http: //localhost/json/exp2.php
مثال 3: طباعة JSON باستخدام خيار JSON_PRETTY_PRINT و tag
The formatting that is applied in the previous example can be done by using ‘ pre ’ tag in place of header() function. Create a file named exp3.php with the following code. In this example, starting the ‘pre’ tag is used before generating JSON data. The output will be similar to the previous example.
exp3.php
<?php$data_arr = array('Robin Nixon' => 'Learning PHP, MySQL and JavaScript ',
'Jon Duckett' => 'HTML & CSS: Design and Build Web Sites', 'Rob Foster' =>
'CodeIgniter 2 Cookbook');
?>
<pre>
<?php
echo json_encode($data_arr, JSON_PRETTY_PRINT);
?>
pre>
Output:
The following output will appear after executing the file from the browser.
http://localhost/json/exp3.php
Example-4: Colorful JSON printing using the custom function
Formatted JSON data are printed by using JSON_PRETTY_PRINT option of PHP in the previous examples. But if you want to print JSON data with the custom format then it is better to use the user-defined function of PHP. How you can apply CSS in JSON data using PHP is mainly shown in this example. Create a PHP file named exp4.php with the following code. A large JSON data is used in this example that is stored in the variable, $data . A user-defined function, pretty_print() is used in the code to format the JSON data. This function has an argument that used to pass the JSON data. A for loop is used in the function to parse the JSON data and apply differently typed of formatting before printing the data.
exp4.php
<?php//Define a large json data
$data = '{'quiz bank':{ 'Computer': {'q1': { 'question': 'who is the inventor of
computer?','options': ['Thomas Alva Edison','Charles Babbage','Blaise Pascal',
'Philo Farnsworth'],'answer': 'Charles Babbage'},{'q2': { 'question':
'which of the following is a input device?', 'options': ['Printer','Scanner',
'Monitor', 'Keyboard'],'answer': 'Keyboard'}},'PHP': { 'q1': { 'question':
'What type of language is PHP?','options': ['High Level Language','Low level
Language','Scripting Language','Assembly Language'],'answer': 'Scripting Language' },
'q2': {'question': 'What is the full form of PHP?','options': ['Hypertext Preprocessor',
'Personal Home Package','Hypertext Processor','Perdonal HTML Page' ],'answer':
'Hypertext Preprocessor'} } } }';
//call custom function for formatting json data
echo pretty_print($data);
//Declare the custom function for formatting
function pretty_print($json_data)
{
//Initialize variable for adding space
$space = 0;
$flag = false;
//Using <pre> tag to format alignment and font
echo '
';
//loop for iterating the full json data
for($counter=0; $counter<strlen($json_data); $counter++)
{
//Checking ending second and third brackets
if ( $json_data[$counter] == '}' || $json_data[$counter] == ']' )
{
$space--;
echo ' ';
echo str_repeat(' ', ($space*2));
}
//Checking for double quote() and comma (,)
if ( $json_data[$counter] == ''' && ($json_data[$counter-1] == ',' ||
$json_data[$counter-2] == ',') )
{
echo ' ';
echo str_repeat(' ', ($space*2));
}
if ( $json_data[$counter] == ''' && !$flag )
$json_data[$counter-2] == ':' )
//Add formatting for question and answer
echo '';
else
//Add formatting for answer options
echo '';
echo $json_data[$counter];
//Checking conditions for adding closing span tag
if ( $json_data[$counter] == ''' && $flag )
echo '';
if ( $json_data[$counter] == ''' )
$flag = !$flag;
//Checking starting second and third brackets
if ( $json_data[$counter] == '{' || $json_data[$counter] == '[' )
{
$space++;
echo ' ';
echo str_repeat(' ', ($space*2));
}
}
echo ' '؛
}
؟>
انتاج:
سيظهر الإخراج التالي بعد تنفيذ الملف من المتصفح. هنا ، ستتم طباعة كل سؤال وإجابة لبيانات JSON أزرق اللون و بالخط العريض التنسيق وجزء آخر ستتم طباعته بامتداد صافي لون.
http: //localhost/json/exp4.php
استنتاج
كيف يمكنك طباعة بيانات JSON المنسقة باستخدام خيارات PHP المتنوعة التي حاولت إظهارها في هذه المقالة. آمل أن يكون القارئ قادرًا على تطبيق PHP لتنسيق بيانات JSON وإنشاء مخرجات JSON جميلة بعد ممارسة الأمثلة المذكورة أعلاه بشكل صحيح.