{"meta":{"title":"调试无效的 JSON","intro":"副驾驶聊天 能够识别并解决 JSON 数据中的语法错误或结构问题。","product":"GitHub Copilot","breadcrumbs":[{"href":"/zh/copilot","title":"GitHub Copilot"},{"href":"/zh/copilot/tutorials","title":"教程"},{"href":"/zh/copilot/tutorials/copilot-chat-cookbook","title":"GitHub Copilot Chat 指南"},{"href":"/zh/copilot/tutorials/copilot-chat-cookbook/debug-errors","title":"调试中出现的错误"},{"href":"/zh/copilot/tutorials/copilot-chat-cookbook/debug-errors/debug-invalid-json","title":"调试无效的 JSON"}],"documentType":"article"},"body":"# 调试无效的 JSON\n\n副驾驶聊天 能够识别并解决 JSON 数据中的语法错误或结构问题。\n\n在处理 JSON 数据时，你可能会遇到诸如尾随逗号、括号不匹配或数据类型错误等问题，这些问题会使 JSON 无效。 GitHub Copilot 聊天功能 可以提供更正建议来修复无效的 JSON，从而帮助调试和解决这些错误。\n\n## 示例方案\n\n考虑这样一个场景：一个应用程序从 API 使用 JSON 数据，但由于格式无效，响应无法分析。 你收到错误消息：\n\n```bash\nError: Parse error\n----------------------^\nExpecting 'STRING', 'NUMBER', 'NULL', 'TRUE', 'FALSE', '{', '[', got 'undefined'\n```\n\n下面是导致错误的 JSON 数据：\n\n```json id=json-error\n{\n  \"location\": \"San Francisco\",\n  \"current_weather\": {\n    \"temperature\": 18,\n    \"unit\": \"Celsius\",\n    \"conditions\": \"Cloudy\n  },\n  \"forecast\": {\n    \"day\": \"Monday\",\n    \"high\": 22,\n    \"low\": 15,\n    \"precipitation\": 10\n  }\n}\n```\n\n## 示例提示\n\n```copilot copy prompt ref=json-error\nWhy is my JSON object invalid and how can I fix it?\n```\n\n## 示例响应\n\n> \\[!NOTE] 以下响应是示例。 副驾驶聊天 的回答是不确定的，因此你可能会得到与这里所显示的不同的回答。\n\nCopilot 可能会提示你的 JSON 无效，因为在 `conditions` 值处缺少一个闭合引号。 下面是更正后的 JSON：\n\n```json\n{\n  \"location\": \"San Francisco\",\n  \"current_weather\": {\n    \"temperature\": 18,\n    \"unit\": \"Celsius\",\n    \"conditions\": \"Cloudy\"\n  },\n  \"forecast\": {\n    \"day\": \"Monday\",\n    \"high\": 22,\n    \"low\": 15,\n    \"precipitation\": 10\n  }\n}\n```\n\n在这个示例响应中，Copilot 的建议包括为 `conditions` 值修复闭合引号，从而解决了 JSON 分析错误。\n\n## 其他阅读材料\n\n* [GitHub Copilot 对话助手的提示设计](/zh/copilot/using-github-copilot/prompt-engineering-for-github-copilot)\n* [使用 GitHub Copilot 的最佳做法](/zh/copilot/using-github-copilot/best-practices-for-using-github-copilot)"}