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);