Write a function that parses a JSON string.
The function should handle one of the three errors below:
- Invalid JSON: The input string is not valid JSON.
- Missing keys: The JSON object is missing expected keys.
- Incorrect data types: The values associated with keys have unexpected data types.
The JSON should have the keys food (string), category (string value of either Asian or Western), and price (integer).
The function should return a dictionary containing the parsed data if successful, or a string error message specifying exactly one of the three categories of errors above.
Possible error strings:
Invalid JSONMissing keysIncorrect data types
Note: Each test will only have one error, so don’t worry about ordering which error should take precedence.
这道题要求实现一个 JSON 解析函数,并在解析结果不合法时返回对应的错误字符串。核心思路是先用 json.loads 尝试把字符串转成字典;如果抛出解析异常,就返回 <code>Invalid JSON</code>。如果解析成功,再检查是否同时包含 <code>food</code>、<code>category</code>、<code>price</code> 三个键,缺少任意一个就返回 <code>Missing keys</code>。最后分别校验字段类型:<code>food</code> 必须是字符串,<code>category</code> 必须是字符串且只能是 <code>Asian</code> 或 <code>Western</code>,<code>price</code> 必须是整数;只要有一项不符合,就返回 <code>Incorrect data types</code>。这题考察的是基础的 JSON 处理、字典键检查和类型判断,逻辑清晰,按顺序逐层校验即可。