[关闭]
@lupnfer 2017-08-30T13:39:54.000000Z 字数 2279 阅读 757

JSONCPP

Code


trans.json

  1. {
  2. "node": "IAS",
  3. "name": "PeoCountingCfg",
  4. "srcCfg": {
  5. "vmInfo": {
  6. "vmAddr": "192.168.0.1",
  7. "vmUser": "loadmin",
  8. "vmPasswd": "-42-49-66-1-242-227-212-197-182-238-222"
  9. },
  10. "camCode": "cam1"
  11. },
  12. "rule": {
  13. "points": [
  14. {
  15. "point": [
  16. 100,
  17. 100
  18. ]
  19. },
  20. {
  21. "point": [
  22. 100,
  23. 200
  24. ]
  25. }
  26. ],
  27. "direction": [
  28. {
  29. "pointStart": [
  30. 100,
  31. 100
  32. ]
  33. },
  34. {
  35. "pointEnd": [
  36. 200,
  37. 100
  38. ]
  39. }
  40. ],
  41. "peopleCalib": [
  42. {
  43. "headY": 20,
  44. "footY": 40
  45. },
  46. {
  47. "headY": 80,
  48. "footY": 120
  49. },
  50. {
  51. "headY": 150,
  52. "footY": 220
  53. }
  54. ],
  55. "number": {
  56. "up": 20,
  57. "down": 10
  58. },
  59. "factor": {
  60. "up": 1.25,
  61. "down": 0.76
  62. }
  63. }
  64. }

video.json

  1. {
  2. "array" : {
  3. "age" : 18,
  4. "array1" : [
  5. {
  6. "key" : 0
  7. },
  8. {
  9. "key" : 1
  10. },
  11. {
  12. "key" : 2
  13. },
  14. {
  15. "key" : 3
  16. },
  17. {
  18. "key" : 4
  19. },
  20. {
  21. "key" : 5
  22. },
  23. {
  24. "key" : 6
  25. },
  26. {
  27. "key" : 7
  28. },
  29. {
  30. "key" : 8
  31. },
  32. {
  33. "key" : 9
  34. }
  35. ],
  36. "map" : [
  37. {
  38. "arrayMap" : "zengraoli"
  39. },
  40. {
  41. "arrayMap" : "zengraoli"
  42. },
  43. {
  44. "arrayMap" : "zengraoli"
  45. },
  46. {
  47. "arrayMap" : "zengraoli"
  48. },
  49. {
  50. "arrayMap" : "zengraoli"
  51. },
  52. {
  53. "arrayMap" : "zengraoli"
  54. },
  55. {
  56. "arrayMap" : "zengraoli"
  57. },
  58. {
  59. "arrayMap" : "zengraoli"
  60. },
  61. {
  62. "arrayMap" : "zengraoli"
  63. },
  64. {
  65. "arrayMap" : "zengraoli"
  66. }
  67. ]
  68. }
  69. }
  1. #include "iostream"
  2. #include "string"
  3. #include "json/json.h"
  4. #include "fstream"
  5. #include "assert.h"
  6. using namespace std;
  7. int ReadFromFile(const string& fileName)
  8. {
  9. ifstream fin;
  10. fin.open(fileName.c_str());
  11. assert(fin.is_open());
  12. Json::Reader reader;
  13. Json::Value root;
  14. if (!reader.parse(fin, root, false))
  15. {
  16. printf("Read Json Error");
  17. return -1;
  18. }
  19. // 获取数组中的数据
  20. cout << "array data:" << endl;
  21. int arraySize = root["array"]["map"].size();
  22. for (int i = 0; i <arraySize; i++)
  23. {
  24. Json::Value valKeyValue = root["array"]["map"][i]["arrayMap"];
  25. string tempKeyValue = valKeyValue.asString();
  26. cout << "array map arrayMap:" << tempKeyValue << endl;
  27. }
  28. cout << endl;
  29. cout << "my age is:" << root["array"]["age"] << endl;
  30. int arraySize2 = root["array"]["array1"].size();
  31. for (int i = 0; i <arraySize2; i++)
  32. {
  33. Json::Value valKeyValue = root["array"]["array1"][i]["key"];
  34. int tempKeyValue = valKeyValue.asInt();
  35. cout << "tempKeyValue:" << tempKeyValue << endl;
  36. }
  37. }
  38. int WriteToFile(const string& fileName)
  39. {
  40. ofstream fout;
  41. fout.open(fileName.c_str());
  42. assert(fout.is_open());
  43. Json::Value root;
  44. Json::Value arrayObj;
  45. Json::Value item;
  46. Json::Value arrayMap;
  47. for (int i = 0; i < 10; i++)
  48. {
  49. item["arrayMap"] = "zengraoli";
  50. arrayMap.append(item);
  51. }
  52. root["array"]["map"]= arrayMap;
  53. root["array"]["age"] = 18;
  54. Json::Value item2;
  55. for (int i = 0; i < 10; i++)
  56. {
  57. item2["key"] = i;
  58. arrayObj.append(item2);
  59. }
  60. root["array"]["array1"] = arrayObj;
  61. std::string out = root.toStyledString();
  62. fout << root.toStyledString() << endl;
  63. return 0;
  64. }
  65. int main(int argc, char* argv[])
  66. {
  67. //WriteToFile("video.json");
  68. ReadFromFile("trans.json");
  69. return 0;
  70. }
添加新批注
在作者公开此批注前,只有你和作者可见。
回复批注