LeetCode 2759: Convert JSON String to Object Solution
Master LeetCode problem 2759 (Convert JSON String to Object), a hard 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.
2759. Convert JSON String to Object
Problem Explanation
Explanation:
To convert a JSON string to an object, we can use a recursive approach. We will start by parsing the JSON string character by character and build the object accordingly. We will handle different JSON data types such as objects, arrays, strings, numbers, booleans, and null values.
Algorithm:
- Initialize a pointer to keep track of the current position while parsing the JSON string.
- Define a recursive function to handle different JSON data types:
- If the current character is '{', the JSON data type is an object. Parse the key-value pairs within the object recursively.
- If the current character is '[', the JSON data type is an array. Parse the elements within the array recursively.
- If the current character is a digit or '-', parse the number value.
- If the current characters are 'true' or 'false', parse the boolean value.
- If the current characters are 'null', parse the null value.
- If the current character is a double quote, parse the string value.
- Return the parsed object.
Time Complexity:
The time complexity of this algorithm is O(n), where n is the length of the JSON string.
Space Complexity:
The space complexity of this algorithm is O(n) due to the recursive calls and the space required for storing the parsed object.
: :
Solution Code
class Solution {
int index = 0;
public Object deserialize(String s) {
if (s == null || s.length() == 0) return null;
return parseValue(s);
}
private Object parseValue(String s) {
char c = s.charAt(index);
if (c == '{') {
return parseObject(s);
} else if (c == '[') {
return parseArray(s);
} else if (c == '"') {
return parseString(s);
} else if (c == 't' || c == 'f') {
return parseBoolean(s);
} else if (c == 'n') {
return parseNull(s);
} else {
return parseNumber(s);
}
}
private Map<String, Object> parseObject(String s) {
Map<String, Object> obj = new HashMap<>();
index++; // Skip '{'
while (s.charAt(index) != '}') {
String key = parseString(s);
index++; // Skip ':'
Object value = parseValue(s);
obj.put(key, value);
if (s.charAt(index) == ',') {
index++; // Skip ','
}
}
index++; // Skip '}'
return obj;
}
private List<Object> parseArray(String s) {
List<Object> arr = new ArrayList<>();
index++; // Skip '['
while (s.charAt(index) != ']') {
arr.add(parseValue(s));
if (s.charAt(index) == ',') {
index++; // Skip ','
}
}
index++; // Skip ']'
return arr;
}
private String parseString(String s) {
StringBuilder sb = new StringBuilder();
index++; // Skip '"'
while (s.charAt(index) != '"') {
sb.append(s.charAt(index++));
}
index++; // Skip '"'
return sb.toString();
}
private boolean parseBoolean(String s) {
if (s.charAt(index) == 't') {
index += 4; // Skip 'true'
return true;
} else {
index += 5; // Skip 'false'
return false;
}
}
private Object parseNull(String s) {
index += 4; // Skip 'null'
return null;
}
private Object parseNumber(String s) {
int start = index;
while (index < s.length() && (Character.isDigit(s.charAt(index)) || s.charAt(index) == '-')) {
index++;
}
String numStr = s.substring(start, index);
if (numStr.contains(".")) {
return Double.parseDouble(numStr);
} else {
return Integer.parseInt(numStr);
}
}
}Try It Yourself
Loading code editor...
Frequently Asked Questions
How to solve LeetCode 2759 (Convert JSON String to Object)?
This page provides optimized solutions for LeetCode problem 2759 (Convert JSON String to Object) 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 2759 (Convert JSON String to Object)?
The time complexity for LeetCode 2759 (Convert JSON String to Object) varies by solution. Check the detailed explanation section for specific complexities in Java, C++, and Python implementations.
Can I run code for LeetCode 2759 on DevExCode?
Yes, DevExCode provides an interactive code editor where you can write, test, and run your code for LeetCode 2759 in Java, C++, or Python.