Given an object, return a valid JSON string of that object. You may assume the object only inludes strings, integers, arrays, objects, booleans, and null. The returned string should not include extra spaces. The order of keys should be the same as the order returned by Object.keys().
Please solve it without using the built-in JSON.stringify method.
給予一個物件,回傳這個物件的一個有效 JSON 字串。 您可以假設該物件僅包含strings, integers, arrays, objects, booleans 和 null。 回傳的字串不應該包含額外的空格,且 key-value 的順序應與 Object.keys() 回傳的順序相同。 請不要使用內建的 JSON.stringify 方法來解決問題。
Example 1:
Input: object = {"y":1,"x":2} Output: {"y":1,"x":2}
Example 2:
Input: object = {"a":"str","b":-12,"c":true,"d":null} Output: {"a":"str","b":-12,"c":true,"d":null}
Example 3:
Input: object = {"key":{"a":1,"b":[{},null,"Hello"]}} Output: {"key":{"a":1,"b":[{},null,"Hello"]}}
Example 4:
Input: object = true Output: true
solution:
自定義一個函式 jsonStringify(),用於將 JavaScript 的物件轉換成 JSON 字串格式。
我們先依據 JavaScript 的型別分為 Arrays []、Objects {}、String “”、Booleans、Numbers 分類處理,
1. 先檢查傳入的物件是否為 null、undefined,如果是則回傳轉成對應的字串。
2. 使用 Array.isArray()來檢查物件是否為陣列,如果是則運用遞迴(再次呼叫jsonStringify(obj))來處理陣列中的每個元素,
將每個元素轉乘 JSON 字串,並以逗號區隔開來,再將整個內容用中括號 [] 包裹後回傳。
3. 使用 typeof 確認是否為一般物件(非陣列、非基本型別),如果是則運用遞迴(再次呼叫jsonStringify(obj))來處理每個 key-value,
並將字串以逗號區隔開來,再將整個內容用大括號 {} 包裹後回傳。
4. 使用 typeof 確認是否為字串,如果是則將字串內容用雙引號 “” 包裹後回傳。
5. 最後的狀態剩下 Booleans or Numbers 則轉成字串進行處理後回傳。
Code 1: Convert Object to JSON String BigO(n)
var jsonStringify = function(object) { if (object === null || object === undefined) return String(object) //Arrays [] if (Array.isArray(object)) { const values = object.map((obj) => jsonStringify(obj)) return `[${values.join(",")}]`; } //Objects {} if (typeof object === "object") { const keys = Object.keys(object) const keyValPairs = keys.map((key) => `"${key}":${jsonStringify(object[key])}`) return `{${keyValPairs.join(",")}}`; } //Strings "" if (typeof object === "string") return `"${String(object)}"`; //Booleans or Numbers return String(object); };
FlowChart:
Example 1
console.log(jsonStringify({"y":1,"x":2})) //{"y":1,"x":2}
Example 2
console.log(jsonStringify({"a":"str","b":-12,"c":true,"d":null})) //{"a":"str","b":-12,"c":true,"d":null}
Example 3
console.log(jsonStringify({"key":{"a":1,"b":[{},null,"Hello"]}})) //{"key":{"a":1,"b":[{},null,"Hello"]}}
Example 4
console.log(jsonStringify(true)) //true