आसान 0 हल किए गए 0% स्वीकृति

Build a JSON Object

Turn keys and values into a JSON object string, escaping quotes and backslashes correctly.

आप क्या सीखेंगे

  • Write key-value pairs in JSON syntax
  • Escape quotes and backslashes inside strings
  • Join parts with a separator

समस्या

अनुवाद दिखाएँ

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 {}.

सीमाएँ

0 ≤ number of pairs ≤ 100

Keys and values have at most 50 printable characters

उदाहरण

उदाहरण 1
इनपुट:
keys = ["name", "city"]
values = ["Ada", "London"]

अपेक्षित आउटपुट:
expected = "{\"name\": \"Ada\", \"city\": \"London\"}"
इनपुट:
std::vector<std::string> keys = {"name", "city"};
std::vector<std::string> values = {"Ada", "London"};

अपेक्षित आउटपुट:
std::string expected = "{\"name\": \"Ada\", \"city\": \"London\"}";
इनपुट:
string[] keys = new string[] {"name", "city"};
string[] values = new string[] {"Ada", "London"};

अपेक्षित आउटपुट:
string expected = "{\"name\": \"Ada\", \"city\": \"London\"}";

व्याख्या

Each pair becomes "key": "value": {"name": "Ada", "city": "London"}.

उदाहरण 2
इनपुट:
keys = ["quote"]
values = ["say \"hi\""]

अपेक्षित आउटपुट:
expected = "{\"quote\": \"say \\\"hi\\\"\"}"
इनपुट:
std::vector<std::string> keys = {"quote"};
std::vector<std::string> values = {"say \"hi\""};

अपेक्षित आउटपुट:
std::string expected = "{\"quote\": \"say \\\"hi\\\"\"}";
इनपुट:
string[] keys = new string[] {"quote"};
string[] values = new string[] {"say \"hi\""};

अपेक्षित आउटपुट:
string expected = "{\"quote\": \"say \\\"hi\\\"\"}";

व्याख्या

The quotes inside the value are escaped with a backslash: {"quote": "say \"hi\""}.

कैसे हल करें

पहले खुद कोशिश करें, फिर एक-एक करके संकेत खोलें।

संकेत 1

Build each "key": "value" pair separately, then join them.

संकेत 2

Escape backslashes before quotes so you don't escape your own escapes.

संकेत 3

An empty input still needs the braces: {}.

अपेक्षित जटिलता

O(total length) time, O(total length) extra space

संदर्भ

यह चुनौती CodeUtility के लिए अनुकूलित की गई है। ऊपर दिए गए स्टेटमेंट और सीमाओं का पालन करें।

दूसरों से सीखें

समुदाय समाधान

अभी तक कोई प्रकाशित समाधान नहीं। पहला साझा करने के लिए इस चुनौती को हल करें।