Output multiple JSON objects

Source: Internet
Author: User
I have three arrays generated from three functions to be made into JSON data return
But if each is echo Json_encode (), and the last data returned is three JSON objects, there is a problem resolving it.
It's best not to put three of arrays together ...
And if the JSON data is returned, the browser should also be able to identify the structure of the JSON data to show it?
What I'm returning here is a direct display of the string form ...
The statement is used
Header (' Content-type:application/json ');

Where could something go wrong?


Reply to discussion (solution)

What kind of JSON data do you need to return?
is unity returned by a method? Then you'll combine the arrays into different JSON strings, such as:
Echo json_enocde (' a ' = $a, ' b ' = = $b, ' C ' + $c));

You said there was a problem. "But if each one is echo json_encode (), the last data returned is three JSON objects, parsing is problematic."
What's the problem?
You will return the value with Var json_obj=json.parse (return data) so that it becomes a JSON object

For the page can be controlled as follows
function Func1 () {}
function Func2 () {}
function func3 () {}

$data = Array ();
$data ["func1"] = Func1 ();
$data ["func2"] = Func2 ();
$data ["func3"] = func3 ();

echo Json_encode ($data);

That would be a good way to parse it.

Although JSON is just a string, he is formatted
If the format is not correct, you cannot parse the

You output 3 JSON strings respectively, to the browser due to the format is not correct, unable to parse the
So we're going to use an array to assemble them together and then output

If you do not want to use the array, then merge into a new object, you want three data as a property, and then one-time response to the client can AH

Thank you for your answer. The solution is to only divide the output of three times the output of the bar was taught

Merge into objects, parse them into JSON, and then take different objects on the client

First put three number combinations into an array, then Json_encode.

Are all data of the same interface, why not spell it together?

PHP returns JSON data example

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