How to obtain two-dimensional JSON in reverse order in chrome

Source: Internet
Author: User

JSON has gradually swallowed up XML space thanks to its simple encoding and decoding method, good readability, scalability, and ambiguous relationship with JavaScript, now it has become the absolute main force of Ajax data exchange. However, if you get the following JSON from the background, this may cause some minor problems in Chrome browser. This is exactly what we will discuss in this article.

VaR people = {"Zhang": {"Age": 28, "location": "Chengdu"}, "Lee": {"Age": 32, "location ": "Kunming "}};

The above code looks like everything is normal. Zhang and Li are from Chengdu and Kunming, respectively, a 28-year-old and a 32-year-old. We have received this JSON and assigned it to the variable people. There is no problem in Firefox, ie, Safari, and opera, but in chrome, this JSON is unfortunately reversed:

VaR people = {"Lee": {"Age": 32, "location": "Kunming"}, "Zhang": {"Age": 28, "location ": "Chengdu "}};

If you know in advance that the returned data contains information about Lee and Zhang, this will not affect your operations. However, if you do not know anything about the returned information, and you need to use for in to obtain the JSON content, it means that in chrome, you will get a set of data in the same order as other browsers. In fact, this is not Chrome's fault. What you need to do at this time is not to reverse the chrome, but to notify the background programmers who work with you to let them return an array for you, the result can be retrieved securely in each browser.

VaR people = ["Zhang": {"Age": 28, "location": "Chengdu"}, "Lee": {"Age": 32, "location ": "Kunming"}];

Taking php as an example, the background needs to be adjusted as follows:

$result = array();for($i = 0 ; $i < 10 ; $i++){    $result[$i]['name'] = 'xxx';    $result[$i]['age'] = 000;}echo json_encode($result);

The last piece of code in this article shows how to extract the comments name from WordPress and return it to the front-end. I hope this will help you.

$result = array();$comments = get_comments();foreach($comments as $key=>$comment){    $result[$key]["authorName"] = $comment -> comment_author;}echo json_encode($result);

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.