# 调试无效的 JSON

副驾驶聊天 能够识别并解决 JSON 数据中的语法错误或结构问题。

在处理 JSON 数据时，你可能会遇到诸如尾随逗号、括号不匹配或数据类型错误等问题，这些问题会使 JSON 无效。 GitHub Copilot 聊天功能 可以提供更正建议来修复无效的 JSON，从而帮助调试和解决这些错误。

## 示例方案

考虑这样一个场景：一个应用程序从 API 使用 JSON 数据，但由于格式无效，响应无法分析。 你收到错误消息：

```bash
Error: Parse error
----------------------^
Expecting 'STRING', 'NUMBER', 'NULL', 'TRUE', 'FALSE', '{', '[', got 'undefined'
```

下面是导致错误的 JSON 数据：

```json id=json-error
{
  "location": "San Francisco",
  "current_weather": {
    "temperature": 18,
    "unit": "Celsius",
    "conditions": "Cloudy
  },
  "forecast": {
    "day": "Monday",
    "high": 22,
    "low": 15,
    "precipitation": 10
  }
}
```

## 示例提示

```copilot copy prompt ref=json-error
Why is my JSON object invalid and how can I fix it?
```

## 示例响应

> \[!NOTE] 以下响应是示例。 副驾驶聊天 的回答是不确定的，因此你可能会得到与这里所显示的不同的回答。

Copilot 可能会提示你的 JSON 无效，因为在 `conditions` 值处缺少一个闭合引号。 下面是更正后的 JSON：

```json
{
  "location": "San Francisco",
  "current_weather": {
    "temperature": 18,
    "unit": "Celsius",
    "conditions": "Cloudy"
  },
  "forecast": {
    "day": "Monday",
    "high": 22,
    "low": 15,
    "precipitation": 10
  }
}
```

在这个示例响应中，Copilot 的建议包括为 `conditions` 值修复闭合引号，从而解决了 JSON 分析错误。

## 其他阅读材料

* [GitHub Copilot 对话助手的提示设计](/zh/copilot/using-github-copilot/prompt-engineering-for-github-copilot)
* [使用 GitHub Copilot 的最佳做法](/zh/copilot/using-github-copilot/best-practices-for-using-github-copilot)