3

这里的某些内容不允许将 '\n' 解释为换行符控制字符。有人可以帮忙吗?

打印时的输出似乎打印

"我希望这个语句\n在换行符中,\n因为我想要它\n那样。",

并不是

“我想要这份声明

在换行符中,

因为我想要

那样。”

static void
append_object_to_array(json_t* my_json_array)
{
    const char* KEY   = "KEY";
    const char* VALUE = "I want this statement\nto be in newlines,\nbecause I want it\n that way.";

    json_t* my_json_obj = json_object();
    json_object_set_new(my_json_obj , KEY, json_string(VALUE));
    json_array_append_new(my_json_array, my_json_obj);
}

int main()
{
    json_t* json_array = json_array();
    
    append_object_to_array(json_array);
    json_decref(my_json_array);

    char* output = NULL;
    output = json_dumps(my_json_array, JSON_ENCODE_ANY);
/* My other failed attempts at getting this to work
 * output = json_dumps(my_json_array, JSON_ESCAPE_SLASH);
 * output = json_dumps(my_json_array, JSON_INDENT(n));
 */

    char* expected_output = "[{\"KEY\": \"I want this statement\nto be in newlines,\nbecause I want it\n that way.\"}]";
    if (strcmp(expected_output, output) != 0) {
        printf("Expected: %s", expected_output);
        printf("\n");
        printf("Actual: %s", output);
    } else {
        printf("Success");
    }

    return 0;
}

PS我已经检查了以下可能的重复项(我可能遗漏了一些东西),但它们似乎不适合我正在寻找的东西。

4

0 回答 0