如果第一个json有错误,Rapidjson解析另一个json



假设我有一个JSON字符串,它有一个错误,因此无法解析。然后我想解析另一个JSON字符串,它将取代原来的字符串。我想使用相同的rapidjson::Document,因为最终我需要在该文档中解析有效的JSON。

:

rapidjson::Document document;
if (document.Parse<0>("{ "hello" : "wor........ ").HasParseError())
{
    // How to parse the correct json "{ "hello" : "world" }" here
    // using the same `Document` ?
}

我是不是应该直接写

if (document.Parse<0>("{ "hello" : "wor........ ").HasParseError())
{
   document.Parse<0>("{ "hello" : "world" }"); 
}

是的,如果首先解析错误,然后使用相同的document解析另一个JSON是OK的,只要它清除该数据并重新解析。

最新更新