PHP for JSON Operation example Analysis, phpjson instance Analysis _php Tutorial

Source: Internet
Author: User
Tags php json php programming

PHP for JSON Operation example Analysis, phpjson instance analysis


This article has analyzed PHP for JSON operations. Share to everyone for your reference. The specific analysis is as follows:

Since JSON can be used in many programming languages, we can use it for small data transfers, such as PHP output JSON strings for JavaScript. In PHP, you can use Json_decode () to parse a JSON object from a string of canonical strings, using Json_encode () to generate a string of canonical strings from a JSON object.

Cases:
Copy the Code code as follows: <?php
$json = ' {' A ': 1, ' B ': 2, ' C ': 3, ' d ': 4, ' E ': 5} ';
Var_dump (Json_decode ($json));
Var_dump (Json_decode ($json, true));

Output:
Copy the Code code as follows: Object (StdClass) #1 (5) {
["a"] = Int (1)
["B"] = + int (2)
["C"] = + int (3)
["D"] = + int (4)
["e"] = + int (5)
}

Array (5) {
["a"] = Int (1)
["B"] = + int (2)
["C"] = + int (3)
["D"] = + int (4)
["e"] = + int (5)
}
Copy the code as follows: $arr = Array (' A ' =>1, ' B ' =>2, ' C ' =>3, ' d ' =>4, ' e ' =>5);
echo Json_encode ($arr);

Output: {"A": 1, "B": 2, "C": 3, "D": 4, "E": 5}

1. Json_decode (), character representable json, is typically used when receiving data sent by JavaScript.
Copy the Code code as follows: <?php
$s = ' {"WebName": "HOMEHF", "url": "Www.homehf.com", "contact": {"QQ": "123456789", "Mail": "nieweihf@163.com", "XX": " xxxxxxx "}}";
$web =json_decode ($s);
Echo ' website name: '. $web->webname. '
URL: '. $web->url. '
Contact: qq-'. $web->contact->qq ' MAIL: '. $web->contact->mail;
?>

In the example above, we first define a variable s, then parse it into a JSON object using Json_decode (), and then use it as JSON, which, from a usage perspective, is similar to the function of JSON and XML and array implementations, and can store some data that exists in relation to each other. But individuals feel that JSON is easier to use, and that data sharing can be achieved using JSON and JavaScript.

2. Json_encode (), JSON-to-character, which is typically used in AJAX applications to convert a JSON object into a string and output it to Javascript, and to store it in a database.
Copy the Code code as follows: <?php
$s = ' {"WebName": "HOMEHF", "url": "Www.homehf.com", "contact": {"QQ": "123456789", "Mail": "nieweihf@163.com", "XX": " xxxxxxx "}}";
$web =json_decode ($s);
echo Json_encode ($web);
?>

Two. PHP JSON to Array
Copy the Code code as follows: <?php
$s = ' {' webname ': ' homehf ', ' url ': ' www.homehf.com ', ' qq ': ' 123456789 '} ';
$web =json_decode ($s); Turn characters into JSON
$arr =array ();
foreach ($web as $k = $w) $arr [$k]= $w;
Print_r ($arr);
?>

In the above code, a JSON object has been turned into an array, but if it is nested JSON, the above code is obviously powerless, then we write a function to solve the nested JSON,

Copy the Code code as follows: <?php
$s = ' {"WebName": "HOMEHF", "url": "Www.homehf.com", "contact": {"QQ": "123456789", "Mail": "nieweihf@163.com", "XX": " xxxxxxx "}}";
$web =json_decode ($s);
$arr =json_to_array ($web);
Print_r ($arr);

function Json_to_array ($web) {
$arr =array ();
foreach ($web as $k = = $w) {
if (Is_object ($w)) $arr [$k]=json_to_array ($W); The judging type is not an object
else $arr [$k]= $w;
}
return $arr;
}
?>

I hope this article is helpful to everyone's PHP programming.

http://www.bkjia.com/PHPjc/941842.html www.bkjia.com true http://www.bkjia.com/PHPjc/941842.html techarticle PHP for JSON operation example Analysis, phpjson example Analysis of the example of PHP for JSON operation. Share to everyone for your reference. The specific analysis is as follows: Since JSON can be used in many ways ...

  • Related Article

    Contact Us

    The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

    If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

    A Free Trial That Lets You Build Big!

    Start building with 50+ products and up to 12 months usage for Elastic Compute Service

    • Sales Support

      1 on 1 presale consultation

    • After-Sales Support

      24/7 Technical Support 6 Free Tickets per Quarter Faster Response

    • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.