json_last_error_msg
json_last_error_msg(): string
把最近一次 JSON 操作的错误码转成人类可读的英文消息,与 json_last_error 配对使用。
C C23
#include <stdio.h>
#include "cJSON.h"
int main(void) {
cJSON *root = cJSON_Parse("{bad");
if (root == NULL) {
/* cJSON 没有消息字符串——只有出错字节位置 */
const char *err = cJSON_GetErrorPtr();
printf("parse failed: %s\n", err != NULL ? "position known" : "no position");
return 0;
}
printf("ok\n");
cJSON_Delete(root);
return 0;
}
parse failed: position known
C++ C++26
#include <iostream>
#include <string>
#include "nlohmann/json.hpp"
int main() {
try {
nlohmann::json::parse("{bad");
std::cout << "ok";
} catch (const nlohmann::json::parse_error& e) {
// e.what() 携带人类可读消息 + 字节位置
std::cout << "has message: " << (std::string(e.what()).size() > 0 ? "true" : "false");
}
return 0;
}
has message: true
C# v11.0.100
try {
System.Text.Json.JsonDocument.Parse("{bad");
Console.WriteLine("ok");
} catch (System.Text.Json.JsonException e) {
// JsonException.Message 带位置详情,与 PHP 8.6 的文案同风格
Console.WriteLine("has message: " + (e.Message.Length > 0));
}
has message: True
Go v1.27.4
package main
import (
"encoding/json"
"errors"
"fmt"
)
func main() {
var v interface{}
err := json.Unmarshal([]byte("{bad"), &v)
fmt.Println("has error:", err != nil)
// errors.As + *json.SyntaxError 是 PHP 侧 JSON_ERROR_SYNTAX(码 4)的类型化对应
var synErr *json.SyntaxError
fmt.Println("is syntax error:", errors.As(err, &synErr))
}
has error: true is syntax error: true
Java v27.0.1
import org.json.JSONObject;
class Main {
public static void main(String[] args) {
try {
new JSONObject("{bad");
System.out.println("ok");
} catch (org.json.JSONException e) {
// getMessage() 就是 json_last_error_msg() 的人类可读对应物
String msg = e.getMessage();
System.out.println("has message: " + (msg != null && !msg.isEmpty()));
}
}
}
has message: true
JavaScript v26.13.1
try {
JSON.parse("{bad");
console.log("ok");
} catch (e) {
console.log(e.name); // SyntaxError - 稳定的身份标识
console.log("has message:", e.message.length > 0 ? true : false);
}
SyntaxError has message: true
PHP v8.6.0
// 任何编解码之前,全局错误状态是「无错误」
var_dump(json_last_error_msg());
json_decode("{bad");
echo "code: ", json_last_error(), "\n"; // 4 = JSON_ERROR_SYNTAX(稳定)
// 消息文案随版本漂移(8.6 起带位置详情);
// 断言稳定的错误族前缀而非整句
var_dump(json_last_error_msg());
echo "prefix ok: ", str_starts_with(json_last_error_msg(), "Syntax error") ? "yes" : "no";
string(8) "No error" code: 4 string(30) "Syntax error near location 1:2" prefix ok: yes
Python v3.15.2
import json
try:
json.loads("{bad")
print("ok")
except json.JSONDecodeError as e:
print(type(e).__name__) # 稳定身份标识(对应 PHP 的错误码)
print("has message:", len(str(e)) > 0) # str(e) 带行/列详情
JSONDecodeError has message: True
Rust v1.99.1
// serde_json:解析失败以 Err 返回;classify() 给出稳定分类
// (对应 PHP 的错误码),Display 就是消息
use serde_json::error::Category;
fn main() {
let result = serde_json::from_str::<serde_json::Value>("{bad");
match result {
Ok(_) => println!("ok"),
Err(e) => {
println!("category syntax: {}", e.classify() == Category::Syntax);
println!("has message: {}", !e.to_string().is_empty());
}
}
}
category syntax: true has message: true
TypeScript v26.13.1
try {
JSON.parse("{bad");
console.log("ok");
} catch (e) {
const err = e as Error; // TS:catch 变量是 unknown,先收窄
console.log(err.name);
console.log("has message:", err.message.length > 0 ? true : false);
}
SyntaxError has message: true
语言差异说明
C:C 标准库没有 JSON——vendor 库 cJSON 只暴露 cJSON_GetErrorPtr 出错字节位置,没有消息字符串(示例如实)。
C++:nlohmann 的 parse_error 带 what() 人类可读消息 + 字节位置,id 字段对应错误码。
C#:JsonException.Message 自带位置详情(行号/字节偏移),与 PHP 8.6 的文案风格同路。
Go:encoding/json 把错误做成类型层级——*json.SyntaxError 等对应 PHP 的错误码常量;errors.As 提取后可拿 Offset。
Java:org.json 抛 JSONException,getMessage() 就是人类可读消息(示例);Jackson 会给更细的 JsonLocation。
JavaScript:JSON.parse 抛 SyntaxError——name 是稳定身份,message 文案随引擎版本漂移(跨版本断言只认 name)。
PHP:错误查询是「全局状态 + 两级口径」——json_last_error 拿稳定错误码,json_last_error_msg 拿文案;文案随版本漂移(8.6 起语法错误带位置),跨版本断言只认码或前缀(示例所写);无操作时返回 No error。
Python:json.JSONDecodeError 的 msg 属性是短消息,str(e) 带行/列位置;异常携带位置属性(lineno/colno)可编程消费。
Rust:serde_json 错误分五个稳定 Category(io/syntax/data/eof/other),对应 PHP 错误码的语义档位;to_string() 是消息。
TypeScript:运行时同 JavaScript;catch 变量默认 unknown 类型,取 message 前先收窄(示例)。