JSON数据构造及解析详解

//2.解析1插入的文件int ParseJsonFromFile(const char* pszFileName){// 解析json用Json::ReaderJson::Reader reader;// Json::Value是一种很重要的类型,,可以代表任意类型。如int, string, object, array…Json::Value root;std::ifstream is;is.open (pszFileName, std::ios::binary );if (reader.parse(is, root)) // reader将Json字符串解析到root,root将包含Json里所有子元素{std::string strUpload_id = root["uploadid"].asString(); // 访问节点,upload_id = "UP000000"int iCode = root["code"].asInt();// 访问节点,code = 100std::string strMsg = root["msg"].asString();cout << "-upload_id = " << strUpload_id << "\t iCode = " << iCode << "\tmsg = " << strMsg << endl;std::string code;if (!root["files"].isNull()) // 访问节点,Access an object value by name, create a null member if it does not exist.{// 得到"files"的数组个数int file_size = root["files"].size();cout << "-file_size = " << file_size << endl;// 遍历数组std::string val_code = root["files"]["code"].asString();std::string val_msg = root["files"]["msg"].asString();std::string val_filename = root["files"]["filename"].asString();std::string val_filesize = root["files"]["filesize"].asString();std::string val_width = root["files"]["width"].asString();std::string val_height = root["files"]["height"].asString();cout << "–code =" << val_code << "\t msg=" << val_msg << "\t filename=" << val_filename \&;< "\t filesize=" << val_filesize << "\t width" << val_width << "\t height =" << val_height << endl << endl;Json::Value val_image = root["files"]["image"];int image_size = val_image.size();cout << "image_size =" << image_size << endl;for(int j = 0; j < image_size; ++j){std::string url = val_image[j]["url"].asString();std::string type = val_image[j]["type"].asString();int width = val_image[j]["width"].asInt();int height = val_image[j]["height"].asInt();cout << "–url =" << url << "\t type =" << type \&;< "\t width =" << width << "\t height =" << height << endl;}}}is.close();return 0;}

世上再美的风景,都不及回家的那段路。

JSON数据构造及解析详解

相关文章:

你感兴趣的文章:

标签云: