{ "data": { "question": { "questionId": "2745", "questionFrontendId": "2633", "categoryTitle": "JavaScript", "boundTopicId": 2222275, "title": "Convert Object to JSON String", "titleSlug": "convert-object-to-json-string", "content": "
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.
\n
Example 1:
\n\n\nInput: object = {"y":1,"x":2}\nOutput: {"y":1,"x":2}\nExplanation: \nReturn the JSON representation.\nNote that the order of keys should be the same as the order returned by Object.keys().\n\n
Example 2:
\n\n\nInput: object = {"a":"str","b":-12,"c":true,"d":null}\nOutput: {"a":"str","b":-12,"c":true,"d":null}\nExplanation:\nThe primitives of JSON are strings, numbers, booleans, and null.\n\n\n
Example 3:
\n\n\nInput: object = {"key":{"a":1,"b":[{},null,"Hello"]}}\nOutput: {"key":{"a":1,"b":[{},null,"Hello"]}}\nExplanation:\nObjects and arrays can include other objects and arrays.\n\n\n
Example 4:
\n\n\nInput: object = true\nOutput: true\nExplanation:\nPrimitive types are valid inputs.\n\n
\n
Constraints:
\n\nobject includes strings, integers, booleans, arrays, objects, and null
1 <= JSON.stringify(object).length <= 105
maxNestingLevel <= 1000
all strings will only contain alphanumeric characters
现给定一个对象,返回该对象的有效 JSON 字符串。你可以假设这个对象只包括字符串、整数、数组、对象、布尔值和 null。返回的字符串不能包含额外的空格。键的返回顺序应该与 Object.keys()
的顺序相同。
请你在不使用内置方法 JSON.stringify
的前提下解决这个问题。
\n\n
示例 1:
\n\n\n输入:object = {\"y\":1,\"x\":2}\n输出:{\"y\":1,\"x\":2}\n解释:\n返回该对象的 JSON 表示形式。\n注意,键的返回顺序应该与 Object.keys() 的顺序相同。\n\n
示例 2:
\n\n\n输入:object = {\"a\":\"str\",\"b\":-12,\"c\":true,\"d\":null}\n输出:{\"a\":\"str\",\"b\":-12,\"c\":true,\"d\":null}\n解释:\nJSON 的基本类型是字符串、数字型、布尔值和 null。\n\n\n
示例 3:
\n\n\n输入:object = {\"key\":{\"a\":1,\"b\":[{},null,\"Hello\"]}}\n输出:{\"key\":{\"a\":1,\"b\":[{},null,\"Hello\"]}}\n解释:\n对象和数组可以包括其他对象和数组。\n\n\n
示例 4:
\n\n\n输入:object = true\n输出:true\n解释:\n基本类型是有效的输入\n\n
\n\n
Constraints:
\n\n对象包括字符串、整数、布尔值、数组、对象和 null
1 <= JSON.stringify(object).length <= 105
maxNestingLevel <= 1000
\\u7248\\u672c\\uff1a \\u60a8\\u7684\\u4ee3\\u7801\\u5728\\u6267\\u884c\\u65f6\\u5c06\\u5e26\\u4e0a lodash.js<\\/a> \\u5e93\\u5df2\\u7ecf\\u9ed8\\u8ba4\\u88ab\\u5305\\u542b\\u3002<\\/p>\\r\\n\\r\\n \\u5982\\u9700\\u4f7f\\u7528\\u961f\\u5217\\/\\u4f18\\u5148\\u961f\\u5217\\uff0c\\u60a8\\u53ef\\u4f7f\\u7528 datastructures-js\\/priority-queue@5.3.0<\\/a> \\u548c datastructures-js\\/queue@4.2.1<\\/a>\\u3002<\\/p>\"],\"typescript\":[\"TypeScript\",\" TypeScript 4.5.4<\\/p>\\r\\n\\r\\n Compile Options: --alwaysStrict --strictBindCallApply --strictFunctionTypes --target ES2020<\\/p>\\r\\n\\r\\n lodash.js<\\/a> \\u5e93\\u5df2\\u7ecf\\u9ed8\\u8ba4\\u88ab\\u5305\\u542b\\u3002<\\/p>\\r\\n\\r\\n \\u5982\\u9700\\u4f7f\\u7528\\u961f\\u5217\\/\\u4f18\\u5148\\u961f\\u5217\\uff0c\\u60a8\\u53ef\\u4f7f\\u7528 datastructures-js\\/priority-queue@5.3.0<\\/a> \\u548c datastructures-js\\/queue@4.2.1<\\/a>\\u3002<\\/p>\"]}",
"book": null,
"isSubscribed": false,
"isDailyQuestion": false,
"dailyRecordStatus": null,
"editorType": "CKEDITOR",
"ugcQuestionId": null,
"style": "LEETCODE",
"exampleTestcases": "{\"y\":1,\"x\":2}\n{\"a\":\"str\",\"b\":-12,\"c\":true,\"d\":null}\n{\"key\":{\"a\":1,\"b\":[{},null,\"Hello\"]}}\ntrue",
"__typename": "QuestionNode"
}
}
}Node.js 16.13.2<\\/code><\\/p>\\r\\n\\r\\n
--harmony<\\/code> \\u6807\\u8bb0\\u6765\\u5f00\\u542f \\u65b0\\u7248ES6\\u7279\\u6027<\\/a>\\u3002<\\/p>\\r\\n\\r\\n