1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156
| /******************************************************** Copyright (C), 2016-2017, FileName: main Author: woniu201 Email: wangpengfei.201@163.com Created: 2017/09/06 Description:use jsoncpp src , not use dll, but i also provide dll and lib. ********************************************************/ #include "stdio.h" #include <string> #include "jsoncpp/json.h" using namespace std; /************************************ @ Brief: read file @ Author: woniu201 @ Created: 2017/09/06 @ Return: file data ************************************/ char *getfileAll(char *fname) { FILE *fp; char *str; char txt[1000]; int filesize; if ((fp=fopen(fname,"r"))==NULL){ printf("open file %s fail \n",fname); return NULL; } fseek(fp,0,SEEK_END); filesize = ftell(fp); str=(char *)malloc(filesize); str[0]=0; rewind(fp); while((fgets(txt,1000,fp))!=NULL){ strcat(str,txt); } fclose(fp); return str; } /************************************ @ Brief: write file @ Author: woniu201 @ Created: 2017/09/06 @ Return: ************************************/ int writefileAll(char* fname,const char* data) { FILE *fp; if ((fp=fopen(fname, "w")) == NULL) { printf("open file %s fail \n", fname); return 1; } fprintf(fp, "%s", data); fclose(fp); return 0; } /************************************ @ Brief: parse json data @ Author: woniu201 @ Created: 2017/09/06 @ Return: ************************************/ int parseJSON(const char* jsonstr) { Json::Reader reader; Json::Value resp; if (!reader.parse(jsonstr, resp, false)) { printf("bad json format!\n"); return 1; } int result = resp["Result"].asInt(); string resultMessage = resp["ResultMessage"].asString(); printf("Result=%d; ResultMessage=%s\n", result, resultMessage.c_str()); Json::Value & resultValue = resp["ResultValue"]; for (int i=0; i<resultValue.size(); i++) { Json::Value subJson = resultValue[i]; string cpuRatio = subJson["cpuRatio"].asString(); string serverIp = subJson["serverIp"].asString(); string conNum = subJson["conNum"].asString(); string websocketPort = subJson["websocketPort"].asString(); string mqttPort = subJson["mqttPort"].asString(); string ts = subJson["TS"].asString(); printf("cpuRatio=%s; serverIp=%s; conNum=%s; websocketPort=%s; mqttPort=%s; ts=%s\n",cpuRatio.c_str(), serverIp.c_str(), conNum.c_str(), websocketPort.c_str(), mqttPort.c_str(), ts.c_str()); } return 0; } /************************************ @ Brief: create json data @ Author: woniu201 @ Created: 2017/09/06 @ Return: ************************************/ int createJSON() { Json::Value req; req["Result"] = 1; req["ResultMessage"] = "200"; Json::Value object1; object1["cpuRatio"] = "4.04"; object1["serverIp"] = "42.159.116.104"; object1["conNum"] = "1"; object1["websocketPort"] = "0"; object1["mqttPort"] = "8883"; object1["TS"] = "1504665880572"; Json::Value object2; object2["cpuRatio"] = "2.04"; object2["serverIp"] = "42.159.122.251"; object2["conNum"] = "2"; object2["websocketPort"] = "0"; object2["mqttPort"] = "8883"; object2["TS"] = "1504665896981"; Json::Value jarray; jarray.append(object1); jarray.append(object2); req["ResultValue"] = jarray; Json::FastWriter writer; string jsonstr = writer.write(req); printf("%s\n", jsonstr.c_str()); writefileAll("createJson.json", jsonstr.c_str()); return 0; } int main() { char* json = getfileAll("parseJson.json"); parseJSON(json); printf("===============================\n"); createJSON(); getchar(); return 1; }
|