-
-
Save ochafik/c0c4bbf14d442122c159c17f37fd96ba to your computer and use it in GitHub Desktop.
URL encode of json
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| function encodeLength(n) { | |
| return String.fromCharCode('A'.charCodeAt(0) + n); | |
| } | |
| function urlEncodeJspbLite(json, fragments = [], prefix="") { | |
| for (const n in json) { | |
| const key = prefix + (n.length != 1 ? encodeLength(n.length) : '') + n; | |
| const value = json[n]; | |
| if (typeof value === 'object') { | |
| urlEncodeJspbLite(value, fragments, key); | |
| } else { | |
| fragments.push(`${key}=${encodeURIComponent(value)}`); | |
| } | |
| } | |
| return fragments; | |
| } | |
| console.log(urlEncodeJspbLite({"2":1,"3":{"1":0,"2":"ca-pub-456"},"11":{"3":1}}).join('&')); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment