Php5.2 and below versions without the json_decode function solution _ PHP Tutorial

Source: Internet
Author: User
Php5.2 and earlier versions do not have the json_decode function solution. When writing code today, we need to use the json_decode function. we found that this function was not integrated in versions earlier than php5.2, but we can implement it through custom functions. Copy code. for example, when writing code today, you need to use the json_decode function. we found that this function was not integrated in php5.2 or earlier versions, but we can implement it through a custom function.

The code is as follows:


Function json_decode2 ($ json)
{
$ Comment = false;
$ Out = '$ x = ';

For ($ I = 0; $ I {
If (! $ Comment)
{
If ($ json [$ I] = '{') | ($ json [$ I] = '[') $ out. = 'Array (';
Else if ($ json [$ I] = '}') | ($ json [$ I] = ']') $ out. = ')';
Else if ($ json [$ I] = ':') $ out. = '=> ';
Else $ out. = $ json [$ I];
}
Else $ out. = $ json [$ I];

If ($ json [$ I] = '"' & $ json [($ i-1)]! = "\") $ Comment =! $ Comment;
}

Eval ($ out .';');
Return $ x;
}

However, Array is returned.

The service_json class is used to return objects.

Bytes. Code such...

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.