PHP JSON operation, Phpjson operation _php Tutorial

Source: Internet
Author: User

PHP JSON operation, Phpjson operation


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:

$json = ' {' A ': 1, ' B ': 2, ' C ': 3, ' d ': 4, ' E ': 5} ';

Var_dump (Json_decode ($json));

Var_dump (Json_decode ($json, true));

Output:

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)
}

$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.

$s = ' {"WebName": "HOMEHF", "url": "Www.homehf.com", "contact": {"QQ": "744348666", "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.

$s = ' {"WebName": "HOMEHF", "url": "Www.homehf.com", "contact": {"QQ": "744348666", "Mail": "nieweihf@163.com", "XX": " xxxxxxx "}}";
$web =json_decode ($s);
echo Json_encode ($web);
?>

Two. PHP JSON to Array

$s = ' {' webname ': ' homehf ', ' url ': ' www.homehf.com ', ' qq ': ' 744348666 '} ';
$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,


$s = ' {"WebName": "HOMEHF", "url": "Www.homehf.com", "contact": {"QQ": "744348666", "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;
}
?>

http://www.bkjia.com/PHPjc/940338.html www.bkjia.com true http://www.bkjia.com/PHPjc/940338.html techarticle PHP JSON operation, Phpjson operation because JSON can be used in many kinds of programming languages, so we can be used to do small data transfer, such as: PHP output JSON string for JavaScript use, etc. ..

  • 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.