LeetCode 2633: Convert Object to JSON String Solution
Master LeetCode problem 2633 (Convert Object to JSON String), a medium challenge, with our optimized solutions in Java, C++, and Python. Explore detailed explanations, test your code in our interactive editor, and prepare for coding interviews.
2633. Convert Object to JSON String
Problem Explanation
Explanation:
To convert an object to a JSON string, we can recursively traverse the object's fields and build the JSON string accordingly. We can handle primitive types directly and for objects or arrays, we can recursively call the function to convert them. We need to handle different types of objects such as arrays, primitive types, nested objects, etc.
Algorithm:
- Create a function that takes an object as input and returns a JSON string.
- If the input object is a primitive type, return the object formatted as a string.
- If the input object is an array, iterate through each element and recursively call the function on each element.
- If the input object is a nested object, recursively call the function on each field of the object.
- Build the JSON string by concatenating the key-value pairs and handling the appropriate formatting for arrays and objects.
Time Complexity: O(N), where N is the total number of elements in the object. Space Complexity: O(N), for the recursive stack space.
:
Solution Code
class Solution {
public String objectToJsonString(Object obj) {
if (obj == null) {
return "null";
}
if (obj instanceof Number || obj instanceof Boolean) {
return obj.toString();
}
if (obj instanceof String) {
return "\"" + obj.toString() + "\"";
}
if (obj instanceof Collection) {
StringBuilder sb = new StringBuilder();
sb.append("[");
boolean first = true;
for (Object elem : (Collection) obj) {
if (!first) {
sb.append(",");
}
sb.append(objectToJsonString(elem));
first = false;
}
sb.append("]");
return sb.toString();
}
if (obj instanceof Map) {
StringBuilder sb = new StringBuilder();
sb.append("{");
boolean first = true;
for (Map.Entry entry : ((Map<?, ?>) obj).entrySet()) {
if (!first) {
sb.append(",");
}
sb.append("\"" + entry.getKey() + "\":");
sb.append(objectToJsonString(entry.getValue()));
first = false;
}
sb.append("}");
return sb.toString();
}
// Handle custom object types here if needed
return null;
}
}Try It Yourself
Loading code editor...
Frequently Asked Questions
How to solve LeetCode 2633 (Convert Object to JSON String)?
This page provides optimized solutions for LeetCode problem 2633 (Convert Object to JSON String) in Java, C++, and Python, along with a detailed explanation and an interactive code editor to test your code.
What is the time complexity of LeetCode 2633 (Convert Object to JSON String)?
The time complexity for LeetCode 2633 (Convert Object to JSON String) varies by solution. Check the detailed explanation section for specific complexities in Java, C++, and Python implementations.
Can I run code for LeetCode 2633 on DevExCode?
Yes, DevExCode provides an interactive code editor where you can write, test, and run your code for LeetCode 2633 in Java, C++, or Python.