# 调试无效的 JSON

Copilot Chat 可以识别和解决 JSON 数据中的语法错误或结构问题。

在处理 JSON 数据时，你可能会遇到诸如尾随逗号、括号不匹配或数据类型错误等问题，这些问题会使 JSON 无效。
GitHub Copilot Chat 可以通过建议修正内容来帮助你调试并修复这些错误，从而修复无效的 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 Chat 的回答是不确定的，因此你可能会得到与这里所显示的不同的回答。

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/concepts/prompting/prompt-engineering)
* [使用 GitHub Copilot 的最佳做法](/zh/copilot/get-started/best-practices)