VC没有自带的JSON库,所以分享一下如何使用该库http://download.csdn.net/detail/moruite/4104704
1. json.h头文件包含了如下API
#ifndef CPPTL_JSON_H_INCLUDED
# define CPPTL_JSON_H_INCLUDED
# include "forwards.h"
# include
# include
# ifndef JSON_USE_CPPTL_SMALLMAP
# include
2. 如何打包json数据
Json::Value rootNode;
Json::Value listNode;
for(i=0;i
3. 如何解析json数据
Json::Value result;
Json::Reader reader;
if(!reader.parse((LPTSTR)(LPCTSTR)strContent,result)){
return FALSE;
}
//m_MachineId.Format("%s",result["machineId"]);
m_MachineId=result["machineId"].asCString();
m_TableId=result["tableId"].asCString();
m_StreamLine=result["streamLine"].asCString();
m_PersonCount=result["personCount"].asCString();
double totalPrice=0;
Json::Value list=result["dishItem"];
if(list.isArray()){
int count=list.size();
Json::Value element;
for(int i=0;idishUnit=element["dishUnit"].asCString();
pItem->dishMake=element["dishMake"].asCString();
pItem->dishId=element["dishId"].asCString();
pItem->dishName=element["dishName"].asCString();
pItem->dishNum.Format("%d",element["dishNum"].asInt());
pItem->dishPrice=element["dishPrice"].asCString();
totalPrice+=atof(element["dishPrice"].asCString());
m_Items.AddTail(pItem);
}
}
m_Price.Format("%.2f",totalPrice);