Parse JSON format.

Source: Internet
Author: User

I'm a beginner in Javascript, so I'm sorry for the following joke.

Sometimes, after obtaining the JSON data, we can only deduce it in the background. if the background is not processed by us, how can we get the format:

// OBJ function jsontostring (OBJ) {var STR = ""; var depth = 0; function jsonparsestring (OBJ) {for (var p in OBJ) {If (object. prototype. tostring. apply (OBJ [p]) = "[object array]") {STR + = P + ":" + "\ n"; var length = OBJ [p]. length; For (VAR I = 0; I <length; I ++) {depth ++; jsonparsestring (OBJ [p] [I]);} else {var con = ""; for (var j = 0; j <depth; j ++) con + = ""; STR + = con + P + "=" + OBJ [p] + "\ n" ;}} depth -- ;} jsonparsestring (OBJ); Return STR ;}

The data is transmitted in JSON format. first, I would like to use a recursive function. however, it is found that the result is incorrect (if there is an array in JSON, the analysis results in the array will not be obtained) mainly because the string in Javascript is a value transfer.

So I want to use global variables. But this function is called for other purposes. It is impossible to write a global variable.
And then use the closure of JavaScript to achieve the corresponding effect without using global variables. This is also true for deep control.
There is no specific test for deep control. If an array exists in the array, or the array cannot be correctly displayed (This test is not performed due to the time relationship)
The resolution result is as follows:
Some people may see it. This is the JSON format required by jqgrid.

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.