Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to transfer php json to object

2025-03-31 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/03 Report--

Today, I will talk to you about how php json should be transferred to object. Many people may not know much about it. In order to make you understand better, the editor has summarized the following contents for you. I hope you can get something according to this article.

Php json to object method: 1, create a PHP sample file; 2, define a "$json" variable; 3, through the "json_decode ($json);" method to get the object object.

This article operating environment: windows7 system, PHP7.1 version, Dell G3 computer.

How to convert php json to object?

Convert a php json string to an array or object

The method found on the Internet is to convert the class type into an array with get_object_vars and then traverse it with foreach.

$array = get_object_vars ($test); $json='[{"id": "1", "name": "\ u5f20\ u96ea\ u6885", "age": "27", "subject": "\ u8ba1\ u7b97\ u673a\ u79d1\ u5b66\ u4e0e\ u6280\ u672f"}, {"id": "2", "name": "\ u5f20\ u6c9b\ u9716", "age": "21", "subject": "\ u8f6f\ u4ef6\ u5de5\ u7a0b"]'

The first step is to encode the string in JSON format with json_decode

$students = json_decode ($json)

Use $students directly in the PHP file:

For ($iTun0 $I object (stdClass) # 2 (4) {["id"] = > string (1) "1" ["name"] = > string (9) "Zhang Xuemei" ["age"] = > string (2) "27" object (stdClass) # 3 (4) {this means that the converted json string is converted to an object rather than an array Please see the red background words ["subject"] = > string (24) "computer Science and Technology"} [1] > ["id"] = > string (1) "2" ["name"] = > string (9) "Zhang Peilin" ["age"] = > string (2) "21" ["subject"] = > string (12) "Software Engineering"}

As you can see, the result returned is object rather than array. Should be accessed as an object:

Foreach ($students as $obj) {echo "name:". $obj- > name. "Age:. $obj- > age." Professional: ". $obj- > subject.";}

The output is as follows:

Name: Zhang Xuemei Age: 27 majors: computer Science and Technology name: Zhang Peilin Age: 21 majors: software Engineering

Mixedjson_decode (string$json [, bool$assoc])

Description: accept a string in JSON format and convert it to a PHP variable.

Json_decode can receive two parameters:

Json: a string in jsonstring format to be decoded.

Assoc: when this parameter is TRUE, array is returned instead of object.

$students = json_decode ($json,true)

At this point, print $students:

Var_dump ($students)

Output:

Array (2) {[0] = > array (4) {["id"] = > string (1) "1" ["name"] = > string (9) "Zhang Xuemei" ["age"] = > string (2) "27" ["subject"] = > string (24) computer Science and Technology} [1] = > array (4) {["id"] = > string (1) "2" ["name"] = > string (9) "Zhang Peilin" ["age"] = > string (2) "21" ["subject"] = > string (12) "Software Engineering"}}

At this point, $students is an array, and you can use it directly:

For ($ixi0witching iname. "Age:". $obj- > age. "Professional:". $obj- > subject. ";}

Method 2:

$json='[{"id": "1", "name": "\ u5f20\ u96ea\ u6885", "age": "27", "subject": "\ u8ba1\ u7b97\ u673a\ u79d1\ u4e0e\ u6280\ u672f"}, {"id": "2", "name": "\ u5f20\ u6c9b\ u9716", "age": "21", "subject": "\ u8f6f\ u4ef6\ u5de5\ u7a0b"]'; $students= json_decode ($json, true); / / arrayfor ($json, true)

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report