Problem
JSON is the text format most programs use to exchange data. A JSON object is written as {"key": "value", ...}.
Given matching lists of keys and values (all text), build the JSON object string in this exact format:
- Start with
{and end with}. - Write each pair as
"key": "value", in the given order, separated by,(comma and space). - Inside a string, write a backslash
\as\\and a double quote"as\". Other characters stay as they are.
Implement json_object_string(keys, values).
Input
keys, values: Lists of the same length.
Output
Return the JSON text. An empty object is {}.
Einschränkungen
0 ≤ number of pairs ≤ 100
Keys and values have at most 50 printable characters
Beispiele
Eingabe:
keys = ["name", "city"]
values = ["Ada", "London"]
Erwartete Ausgabe:
expected = "{\"name\": \"Ada\", \"city\": \"London\"}"
Eingabe:
std::vector<std::string> keys = {"name", "city"};
std::vector<std::string> values = {"Ada", "London"};
Erwartete Ausgabe:
std::string expected = "{\"name\": \"Ada\", \"city\": \"London\"}";
Eingabe:
string[] keys = new string[] {"name", "city"};
string[] values = new string[] {"Ada", "London"};
Erwartete Ausgabe:
string expected = "{\"name\": \"Ada\", \"city\": \"London\"}";
Erläuterung
Each pair becomes "key": "value": {"name": "Ada", "city": "London"}.
Eingabe:
keys = ["quote"]
values = ["say \"hi\""]
Erwartete Ausgabe:
expected = "{\"quote\": \"say \\\"hi\\\"\"}"
Eingabe:
std::vector<std::string> keys = {"quote"};
std::vector<std::string> values = {"say \"hi\""};
Erwartete Ausgabe:
std::string expected = "{\"quote\": \"say \\\"hi\\\"\"}";
Eingabe:
string[] keys = new string[] {"quote"};
string[] values = new string[] {"say \"hi\""};
Erwartete Ausgabe:
string expected = "{\"quote\": \"say \\\"hi\\\"\"}";
Erläuterung
The quotes inside the value are escaped with a backslash: {"quote": "say \"hi\""}.
So gehst du vor
Versuch es zuerst allein und öffne dann einen Hinweis nach dem anderen.
Hinweis 1
Build each "key": "value" pair separately, then join them.
Hinweis 2
Escape backslashes before quotes so you don't escape your own escapes.
Hinweis 3
An empty input still needs the braces: {}.
Erwartete Komplexität
O(total length) time, O(total length) extra space
Referenzen
Diese Aufgabe wurde für CodeUtility angepasst. Beachte die oben genannten Vorgaben und Einschränkungen.