如何找到字符串是 Json 或不使用 Jansson

How to find a string is Json or not using Jansson?

本文关键字:Jansson Json 何找 字符串      更新时间:2023-10-16

我正在使用杨松。

bool ConvertJsontoString(string inputText, string& OutText)
{
    /* Before doing anything I want to check
       if the inputText is a valid json string or not */ 
}

你为什么不阅读文档,其中明确指出:

json_t *json_loads(const char *input, size_t flags, json_error_t *error)
    Return value: New reference.
    Decodes the JSON string input and returns the array or object it contains, 
    or NULL on error, in which case error is filled with information about the error. 
    flags is described above.

此外,他们甚至提供了一个如何使用它的示例:

root = json_loads(text, 0, &error);
free(text);
if(!root)
{
    fprintf(stderr, "error: on line %d: %sn", error.line, error.text);
    return 1;
}