PHP JSON string converted to an array or object

Source: Internet
Author: User
Tags php json string format

The web-based approach is to use Get_object_vars to convert the class type to an array and then use the foreach traversal to

$array = Get_object_vars ($test);

$json = ' [{' id ': ' 1 ', ' name ': ' \u5f20\u96ea\u6885 ', ' age ': ', ' subject ': ' \u8ba1\u7b97\u673a\u79d1\u5b66\u4e0e\u6280 \u672f "},{" id ":" 2 "," Name ":" \u5f20\u6c9b\u9716 "," Age ":" + "," Subject ":" \U8F6F\U4EF6\U5DE5\U7A0B "}] ';

The first thing to do is to encode the JSON-formatted string with Json_decode,

$students = Json_decode ($json);

Directly in PHP file with $students:

for ($i =0; $i <count ($students); $i + +) {
echo "Name:". $students [$i] [' name ']. " Age: ". $students [$i] [' ages ']." Major: ". $students [$i] [' Subject ']." <br/> ";
}

The error is as follows:

Fatal Error: Cannot use Objectof type StdClass as array in D:\wamp\www\test.phpon line

Print $students:

Var_dump ($students);

Will output:

Array (2) {

[0]=>

Object (StdClass) #2 (4) {

["id"]=> string (1) "1"

["Name"]=> string (9) "Ms. Zhang"

["Age"]=> string (2) "27"

object (StdClass) #3 (4) {This means that the converted JSON string goes to an object rather than an array, see the following red background word

["Subject"]=>string (24) "Computer Science and Technology"

}

[1]=>

["id"]=> string (1) "2"

["Name"]=> string (9) "Zhang Yi-lin"

["Age"]=> string (2) "21"

["Subject"]=> string (12) "Software Engineering"

}

}

Visible, the returned result is an object instead of an array. should be accessed as an object:

foreach ($students as $obj) {
echo "Name:". $obj->name. " Age: ". $obj->age." Major: ". $obj->subject." <br/> ";
}

The output is:

Name: Ms. Zhang Age: 27 Major: Computer Science and technology
Name: Zhang Yi-lin Age: 21 Major: Software engineering

MixedJson_decode (String$json [, Bool$assoc])

Description: Accepts a JSON-formatted string and converts it to a PHP variable.

Json_decode can receive two parameters:

JSON: The JSONstring format to be decoded.

Assoc: When this argument is TRUE, an array is returned instead of an object.

$students = Json_decode ($json, true);

Then print the $students:

Var_dump ($students);

Output:

Array (2) {

[0]=>

Array (4) {

["id"]=> string (1) "1"

["Name"]=> string (9) "Ms. Zhang"

["Age"]=> string (2) "27"

["Subject"]=>string (24) "Computer Science and Technology"

}

[1]=>

Array (4) {

["id"]=> string (1) "2"

["Name"]=> string (9) "Zhang Yi-lin"

["Age"]=> string (2) "21"

["Subject"]=>string (12) "Software Engineering"

}

}

At this point, $students is the number of groups, you can directly use:

for ($i =0; $i <count ($students); $i + +) {
echo "Name:". $students [$i] [' name ']. " Age: ". $students [$i] [' ages ']." Major: ". $students [$i] [' Subject ']." <br/> ";
}

The output is:

Name: Ms. Zhang Age: 27 Major: Computer Science and technology
Name: Zhang Yi-lin Age: 21 Major: Software engineering

Summarize:

There are two ways to work with JSON-formatted strings in PHP code:

Method One:

$json = ' [{' id ': ' 1 ', ' name ': ' \u5f20\u96ea\u6885 ', ' age ': ', ' subject ': ' \u8ba1\u7b97\u673a\u79d1\u5b66\u4e0e\u6280 \u672f "},{" id ":" 2 "," Name ":" \u5f20\u6c9b\u9716 "," Age ":" + "," Subject ":" \U8F6F\U4EF6\U5DE5\U7A0B "}] ';

$students = Json_decode ($json);//Get an Object

foreach ($studentsas $obj) {

echo "Name:". $obj->name. " &nbsp;&nbsp;&nbsp; Age: ". $obj->age." &nbsp;&nbsp;&nbsp; major: ". $obj->subject." <br/> ";
}

Method Two:

$json = ' [{' id ': ' 1 ', ' name ': ' \u5f20\u96ea\u6885 ', ' age ': ', ' subject ': ' \u8ba1\u7b97\u673a\u79d1\u5b66\u4e0e\u6280 \u672f "},{" id ":" 2 "," Name ":" \u5f20\u6c9b\u9716 "," Age ":" + "," Subject ":" \U8F6F\U4EF6\U5DE5\U7A0B "}] ';

$students = Json_decode ($json, true);//Get an array

for ($i =0; $i <count ($students); $i + +) {
echo "Name:". $students [$i] [' name ']. " &nbsp;&nbsp;&nbsp; Age: ". $students [$i] [' ages ']." &nbsp;&nbsp;&nbsp; major: ". $students [$i] [' Subject ']." <br/> ";
}

PHP JSON string converted to an array or object

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.