simpledom.cpp 685 B

123456789101112131415161718192021222324252627282930
  1. // JSON simple example
  2. // This example does not handle errors.
  3. #include "rapidjson/document.h"
  4. #include "rapidjson/writer.h"
  5. #include "rapidjson/stringbuffer.h"
  6. #include <iostream>
  7. using namespace rapidjson;
  8. int main() {
  9. // 1. Parse a JSON string into DOM.
  10. const char* json = "{\"project\":\"rapidjson\",\"stars\":10}";
  11. Document d;
  12. d.Parse(json);
  13. // 2. Modify it by DOM.
  14. Value& s = d["stars"];
  15. s.SetInt(s.GetInt() + 1);
  16. // 3. Stringify the DOM
  17. StringBuffer buffer;
  18. Writer<StringBuffer> writer(buffer);
  19. d.Accept(writer);
  20. // Output {"project":"rapidjson","stars":11}
  21. std::cout << buffer.GetString() << std::endl;
  22. return 0;
  23. }