Advertisement
Guest User

Untitled

a guest
Jul 9th, 2013
113
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.38 KB | None | 0 0
  1. #include <iostream>
  2. #include <string>
  3. #include <jsoncpp/json.h>
  4.  
  5. using namespace std;
  6.  
  7. void testJsonCpp() {
  8. std::string s = "{\"col\": \"5\", \"par1\":{\"type\": \"int\",\"value\": \"val\",\"typeGen\": 1}}";
  9. // Let's parse it
  10. Json::Value root;
  11. Json::Reader reader;
  12. bool parsedSuccess = reader.parse(s,
  13. root,
  14. false);
  15.  
  16. if (not parsedSuccess) {
  17. // Report failures and their locations
  18. // in the document.
  19. cout << "Failed to parse JSON" << endl
  20. << reader.getFormatedErrorMessages()
  21. << endl;
  22. return;
  23. }
  24.  
  25. Json::Value::Members mem = root.getMemberNames();
  26. Json::Value child = root[mem[0]];
  27. cout << "name: " << mem[0] << " child: " << child.asString() << endl;
  28. for (int i = 1; i < mem.size(); ++i)
  29. {
  30. child = root[mem[i]];
  31. cout << mem[i] << " " << i << " child: " << child.asString() << endl;
  32. Json::Value::Members childMem = child.getMemberNames();
  33. cout << child[childMem[0]].asString() << endl << "type";
  34. cout << child[childMem[1]].asString() << endl << "value";
  35. cout << child[childMem[2]].asString() << endl << "typeGen";
  36. }
  37.  
  38. // // Let's extract the array contained
  39. // // in the root object
  40. // const Json::Value array = root["name"];
  41. //
  42. // // Iterate over sequence elements and
  43. // // print its values
  44. // for (unsigned int index = 0; index < array.size();
  45. // ++index) {
  46. // cout << "Element "
  47. // << index
  48. // << " in array: "
  49. // << array[index].asString()
  50. // << endl;
  51. // }
  52. //
  53. // // Lets extract the not array element
  54. // // contained in the root object and
  55. // // print its value
  56. // const Json::Value notAnArray =
  57. // root["not an array"];
  58. //
  59. // if (not notAnArray.isNull()) {
  60. // cout << "Not an array: "
  61. // << notAnArray.asString()
  62. // << endl;
  63. // }
  64. //
  65. // // If we want to print JSON is as easy as doing:
  66. // cout << "Json Example pretty print: "
  67. // << endl << root.toStyledString()
  68. // << endl;
  69. }
  70.  
  71. extern "C"
  72. char * func(char *_s) {
  73. std::cout << "print cpp extern func 'C': " << _s << std::endl;
  74.  
  75. testJsonCpp();
  76.  
  77. return _s;
  78. }
  79.  
  80. //g++ -fPIC test.cpp -o test.so -shared
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement