Dễ 1 người đã giải Tỷ lệ chấp nhận 20.0%

Build a JSON Object

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

Bạn sẽ học được gì

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

Đề bài

Xem bản dịch

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

Ràng buộc

0 ≤ number of pairs ≤ 100

Keys and values have at most 50 printable characters

Ví dụ

Ví dụ 1
Đầu vào:
keys = ["name", "city"]
values = ["Ada", "London"]

Kết quả mong đợi:
expected = "{\"name\": \"Ada\", \"city\": \"London\"}"
Đầu vào:
std::vector<std::string> keys = {"name", "city"};
std::vector<std::string> values = {"Ada", "London"};

Kết quả mong đợi:
std::string expected = "{\"name\": \"Ada\", \"city\": \"London\"}";
Đầu vào:
string[] keys = new string[] {"name", "city"};
string[] values = new string[] {"Ada", "London"};

Kết quả mong đợi:
string expected = "{\"name\": \"Ada\", \"city\": \"London\"}";

Giải thích

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

Ví dụ 2
Đầu vào:
keys = ["quote"]
values = ["say \"hi\""]

Kết quả mong đợi:
expected = "{\"quote\": \"say \\\"hi\\\"\"}"
Đầu vào:
std::vector<std::string> keys = {"quote"};
std::vector<std::string> values = {"say \"hi\""};

Kết quả mong đợi:
std::string expected = "{\"quote\": \"say \\\"hi\\\"\"}";
Đầu vào:
string[] keys = new string[] {"quote"};
string[] values = new string[] {"say \"hi\""};

Kết quả mong đợi:
string expected = "{\"quote\": \"say \\\"hi\\\"\"}";

Giải thích

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

Hướng tiếp cận

Hãy tự thử trước, sau đó mở từng gợi ý một.

Gợi ý 1

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

Gợi ý 2

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

Gợi ý 3

An empty input still needs the braces: {}.

Độ phức tạp mong đợi

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

Tham khảo

Thử thách này đã được điều chỉnh cho CodeUtility. Hãy làm theo đề bài và các ràng buộc ở trên.

Lời giải đạt tốt nhất của mỗi người. Lời giải nhanh hơn, tốn ít bộ nhớ hơn và được đánh giá cao hơn sẽ đứng trên.

# Người giải Ngôn ngữ Thời gian Bộ nhớ AI đánh giá Lời giải
1 RCRiccardo Corti Python 830 ms - 4/5 Xem code

Học hỏi từ người khác

Lời giải cộng đồng

Cảnh báo: Có 1 lời giải đầy đủ bên dưới Hãy thử tự giải trước. Bạn vẫn có thể xem code của người khác và nhận xét của AI. Đăng nhập và giải thử thách để tham gia thảo luận.