JSON样例
#include<iostream> #include <string> #include "json.h" using namespace std; using namespace Json; void readJson() { std::string strValue = "{\"name\":\"json\",\"array\":[{\"cpp\":\"jsoncpp\"},{\"java\":\"jsoninjava\"},{\"php\":\"support\"}]}"; Json::Reader reader; Json::Value value; if (reader.parse(strValue, value)) { std::string out = value["name"].asString(); std::cout << out << std::endl; const Json::Value arrayObj = value["array"]; for (unsigned int i = 0; i < arrayObj.size(); i++) { if (!arrayObj[i].isMember("cpp")) continue; out = arrayObj[i]["cpp"].asString(); std::cout << out; if (i != (arrayObj.size() - 1)) std::cout << std::endl; } } } void writeJson() { Json::Value root; Json::Value arrayObj; Json::Value item; item["cpp"] = "jsoncpp"; item["java"] = "jsoninjava"; item["php"] = "support"; arrayObj.append(item); root["name"] = "json"; root["array"] = arrayObj; root.toStyledString(); std::string out = root.toStyledString(); std::cout << out << std::endl; } int main() { readJson(); cout << "---------------------------------------------" << endl; writeJson(); return 0; }
处理下中央气象台返回的json数据:
#include<iostream> #include <string> #include "json.h" using namespace std; using namespace Json; void readJson() { Json::Reader reader; Json::Value value; Json::FastWriter fast_write; Json::StyledWriter style_write; std::string strValue = "{\"weatherinfo\":[{\"city\":\"北京\",\"city_en\":\"beijing\",\"date_y\":\"2013年2月16日\",\"date\":\"\",\"week\":\"星期六\",\"fchh\":\"08\",\"cityid\":\"101010100\",\"temp1\":\"4℃~-3℃\",\"temp2\":\"6℃~-4℃\",\"temp3\":\"3℃~-8℃\",\"temp4\":\"3℃~-6℃\",\"temp5\":\"4℃~-6℃\",\"temp6\":\"4℃~-5℃\",\"tempF1\":\"39.2℉~26.6℉\",\"tempF2\":\"42.8℉~24.8℉\",\"tempF3\":\"37.4℉~17.6℉\",\"tempF4\":\"37.4℉~21.2℉\",\"tempF5\":\"39.2℉~21.2℉\",\"tempF6\":\"39.2℉~23℉\",\"weather1\":\"多云转阴\",\"weather2\":\"多云转晴\",\"weather3\":\"晴\",\"weather4\":\"晴\",\"weather5\":\"晴转多云\",\"weather6\":\"多云转晴\",\"img1\":\"1\",\"img2\":\"2\",\"img3\":\"1\",\"img4\":\"0\",\"img5\":\"0\",\"img6\":\"99\",\"img7\":\"0\",\"img8\":\"99\",\"img9\":\"0\",\"img10\":\"1\",\"img11\":\"1\",\"img12\":\"0\",\"img_single\":\"1\",\"img_title1\":\"多云\",\"img_title2\":\"阴\",\"img_title3\":\"多云\",\"img_title4\":\"晴\",\"img_title5\":\"晴\",\"img_title6\":\"晴\",\"img_title7\":\"晴\",\"img_title8\":\"晴\",\"img_title9\":\"晴\",\"img_title10\":\"多云\",\"img_title11\":\"多云\",\"img_title12\":\"晴\",\"img_title_single\":\"多云\",\"wind1\":\"微风\",\"wind2\":\"北风3-4级\",\"wind3\":\"北风3-4级转微风\",\"wind4\":\"微风\",\"wind5\":\"微风\",\"wind6\":\"微风\",\"fx1\":\"微风\",\"fx2\":\"微风\",\"fl1\":\"小于3级\",\"fl2\":\"3-4级\",\"fl3\":\"3-4级转小于3级\",\"fl4\":\"小于3级\",\"fl5\":\"小于3级\",\"fl6\":\"小于3级\",\"index\":\"冷\",\"index_d\":\"天气冷,建议着棉服、羽绒服、皮夹克加羊毛衫等冬季服装。年老体弱者宜着厚棉衣、冬大衣或厚羽绒服。\",\"index48\":\"寒冷\",\"index48_d\":\"天气寒冷,建议着厚羽绒服、毛皮大衣加厚毛衣等隆冬服装。年老体弱者尤其要注意保暖防冻。\",\"index_uv\":\"最弱\",\"index48_uv\":\"最弱\",\"index_xc\":\"适宜\",\"index_tr\":\"适宜\",\"index_co\":\"较不舒适\",\"st1\":\"4\",\"st2\":\"-2\",\"st3\":\"5\",\"st4\":\"-3\",\"st5\":\"2\",\"st6\":\"-7\",\"index_cl\":\"适宜\",\"index_ls\":\"基本适宜\",\"index_ag\":\"极不易发\"}]}"; if (reader.parse(strValue, value)) { //std::cout << fast_write.write(value) << std::endl; std::cout << style_write.write(value) << std::endl; } } int main() { readJson(); return 0; }输出结果:
{ "weatherinfo" : [ { "city" : "北京", "city_en" : "beijing", "cityid" : "101010100", "date" : "", "date_y" : "2013年2月16日", "fchh" : "08", "fl1" : "小于3级", "fl2" : "3-4级", "fl3" : "3-4级转小于3级", "fl4" : "小于3级", "fl5" : "小于3级", "fl6" : "小于3级", "fx1" : "微风", "fx2" : "微风", "img1" : "1", "img10" : "1", "img11" : "1", "img12" : "0", "img2" : "2", "img3" : "1", "img4" : "0", "img5" : "0", "img6" : "99", "img7" : "0", "img8" : "99", "img9" : "0", "img_single" : "1", "img_title1" : "多云", "img_title10" : "多云", "img_title11" : "多云", "img_title12" : "晴", "img_title2" : "阴", "img_title3" : "多云", "img_title4" : "晴", "img_title5" : "晴", "img_title6" : "晴", "img_title7" : "晴", "img_title8" : "晴", "img_title9" : "晴", "img_title_single" : "多云", "index" : "冷", "index48" : "寒冷", "index48_d" : "天气寒冷,建议着厚羽绒服、毛皮大衣加厚毛衣等隆冬服装。年 老体弱者尤其要注意保暖防冻。", "index48_uv" : "最弱", "index_ag" : "极不易发", "index_cl" : "适宜", "index_co" : "较不舒适", "index_d" : "天气冷,建议着棉服、羽绒服、皮夹克加羊毛衫等冬季服装。年老 体弱者宜着厚棉衣、冬大衣或厚羽绒服。", "index_ls" : "基本适宜", "index_tr" : "适宜", "index_uv" : "最弱", "index_xc" : "适宜", "st1" : "4", "st2" : "-2", "st3" : "5", "st4" : "-3", "st5" : "2", "st6" : "-7", "temp1" : "4℃~-3℃", "temp2" : "6℃~-4℃", "temp3" : "3℃~-8℃", "temp4" : "3℃~-6℃", "temp5" : "4℃~-6℃", "temp6" : "4℃~-5℃", "tempF1" : "39.2℉~26.6℉", "tempF2" : "42.8℉~24.8℉", "tempF3" : "37.4℉~17.6℉", "tempF4" : "37.4℉~21.2℉", "tempF5" : "39.2℉~21.2℉", "tempF6" : "39.2℉~23℉", "weather1" : "多云转阴", "weather2" : "多云转晴", "weather3" : "晴", "weather4" : "晴", "weather5" : "晴转多云", "weather6" : "多云转晴", "week" : "星期六", "wind1" : "微风", "wind2" : "北风3-4级", "wind3" : "北风3-4级转微风", "wind4" : "微风", "wind5" : "微风", "wind6" : "微风" } ] } 请按任意键继续. . .
#include<iostream> #include <string> #include "json.h" using namespace std; using namespace Json; void readJson() { Json::Reader reader; Json::Value value; Json::FastWriter fast_write; Json::StyledWriter style_write; std::string strValue = "{\"weatherinfo\":{\"city\":\"北京\",\"city_en\":\"beijing\",\"date_y\":\"2013年2月16日\",\"date\":\"\",\"week\":\"星期六\",\"fchh\":\"08\",\"cityid\":\"101010100\",\"temp1\":\"4℃~-3℃\",\"temp2\":\"6℃~-4℃\",\"temp3\":\"3℃~-8℃\",\"temp4\":\"3℃~-6℃\",\"temp5\":\"4℃~-6℃\",\"temp6\":\"4℃~-5℃\",\"tempF1\":\"39.2℉~26.6℉\",\"tempF2\":\"42.8℉~24.8℉\",\"tempF3\":\"37.4℉~17.6℉\",\"tempF4\":\"37.4℉~21.2℉\",\"tempF5\":\"39.2℉~21.2℉\",\"tempF6\":\"39.2℉~23℉\",\"weather1\":\"多云转阴\",\"weather2\":\"多云转晴\",\"weather3\":\"晴\",\"weather4\":\"晴\",\"weather5\":\"晴转多云\",\"weather6\":\"多云转晴\",\"img1\":\"1\",\"img2\":\"2\",\"img3\":\"1\",\"img4\":\"0\",\"img5\":\"0\",\"img6\":\"99\",\"img7\":\"0\",\"img8\":\"99\",\"img9\":\"0\",\"img10\":\"1\",\"img11\":\"1\",\"img12\":\"0\",\"img_single\":\"1\",\"img_title1\":\"多云\",\"img_title2\":\"阴\",\"img_title3\":\"多云\",\"img_title4\":\"晴\",\"img_title5\":\"晴\",\"img_title6\":\"晴\",\"img_title7\":\"晴\",\"img_title8\":\"晴\",\"img_title9\":\"晴\",\"img_title10\":\"多云\",\"img_title11\":\"多云\",\"img_title12\":\"晴\",\"img_title_single\":\"多云\",\"wind1\":\"微风\",\"wind2\":\"北风3-4级\",\"wind3\":\"北风3-4级转微风\",\"wind4\":\"微风\",\"wind5\":\"微风\",\"wind6\":\"微风\",\"fx1\":\"微风\",\"fx2\":\"微风\",\"fl1\":\"小于3级\",\"fl2\":\"3-4级\",\"fl3\":\"3-4级转小于3级\",\"fl4\":\"小于3级\",\"fl5\":\"小于3级\",\"fl6\":\"小于3级\",\"index\":\"冷\",\"index_d\":\"天气冷,建议着棉服、羽绒服、皮夹克加羊毛衫等冬季服装。年老体弱者宜着厚棉衣、冬大衣或厚羽绒服。\",\"index48\":\"寒冷\",\"index48_d\":\"天气寒冷,建议着厚羽绒服、毛皮大衣加厚毛衣等隆冬服装。年老体弱者尤其要注意保暖防冻。\",\"index_uv\":\"最弱\",\"index48_uv\":\"最弱\",\"index_xc\":\"适宜\",\"index_tr\":\"适宜\",\"index_co\":\"较不舒适\",\"st1\":\"4\",\"st2\":\"-2\",\"st3\":\"5\",\"st4\":\"-3\",\"st5\":\"2\",\"st6\":\"-7\",\"index_cl\":\"适宜\",\"index_ls\":\"基本适宜\",\"index_ag\":\"极不易发\"}}"; if (reader.parse(strValue, value)) { //std::cout << style_write.write(value) << std::endl; std::cout << value["weatherinfo"]["city"].asString() << endl; std::cout << value["weatherinfo"]["date_y"].asString() << endl; std::cout << value["weatherinfo"]["week"].asString() << endl; } } int main() { readJson(); return 0; }输出:
北京 2013年2月16日 星期六 请按任意键继续. . .
Keep it simple!