Commit d22c95c5 by guanghui.cui

新订单json解析

parent 48994c32
#ifndef BASE_DEFINE_H
#define BASE_DEFINE_H
#define IN
#define OUT
//订单状态对照表
#define ORDERSTATUS_NEW 1001 //新生成
#define ORDERSTATUS_CONFIRMED 1002 //已确定
......
......@@ -66,10 +66,113 @@ JsonModule::~JsonModule()
}
orderObj JsonModule::convertToOrderObj(const char* json)
void JsonModule::convertToOrderObj(IN const char* json,OUT std::vector<orderObj> &vecOrders)
{
orderObj obj;
return obj;
rapidjson::Document document; // 定义一个Document对象
document.Parse(json); // 解析,Parse()无返回值,也不会抛异常
if (document.HasParseError()) // 通过HasParseError()来判断解析是否成功
{
// 可通过GetParseError()取得出错代码,
// 注意GetParseError()返回的是一个rapidjson::ParseErrorCode类型的枚举值
// 使用函数rapidjson::GetParseError_En()得到错误码的字符串说明,这里的En为English简写
// 函数GetErrorOffset()返回出错发生的位置
LOG(ERROR)<<"JSON parse error:"<<document.GetParseError()<<":"<<document.GetErrorOffset();
}
else
{
rapidjson::Value& vCode = document["code"];
rapidjson::Value& vMsg = document["message"];
rapidjson::Value& result_obj = document["result"];
std::string strCode = vCode.GetString();
std::string strMsg = vMsg.GetString();
if(result_obj.IsObject()){
rapidjson::Value& orders_array = result_obj["orders"];
if(orders_array.IsArray()){
for(unsigned int i=0;i<orders_array.Size();i++){
orderObj order;
rapidjson::Value& objValue = orders_array[i];
rapidjson::Value& channel = objValue["channel"];
order.channel=channel.GetString();
rapidjson::Value& order_id = objValue["order_id"];
order.order_id=order_id.GetString();
rapidjson::Value& create_time = objValue["create_time"];
order.create_time=create_time.GetString();
rapidjson::Value& delivery_time = objValue["delivery_time"];
order.delivery_time=delivery_time.GetString();
rapidjson::Value& status = objValue["status"];
order.status=status.GetInt();
rapidjson::Value& status_desc = objValue["status_desc"];
order.status_desc=status_desc.GetString();
rapidjson::Value& total_fee = objValue["total_fee"];
order.total_price=total_fee.GetInt();
rapidjson::Value& send_fee = objValue["send_fee"];
order.delivery_price=send_fee.GetInt();
rapidjson::Value& discount_fee = objValue["discount_fee"];
order.reduced_price=discount_fee.GetInt();
//配送
rapidjson::Value& delivery_type = objValue["delivery_type"];
order.deliveryInfo.type=_getDeliveryTypeString(delivery_type.GetInt());
rapidjson::Value& delivery_status = objValue["delivery_status"];
order.deliveryInfo.status=delivery_status.GetInt();
rapidjson::Value& courier_name = objValue["courier_name"];
order.deliveryInfo.driver_name=courier_name.GetString();
rapidjson::Value& courier_phone = objValue["courier_phone"];
order.deliveryInfo.driver_phone=courier_phone.GetString();
//顾客信息
rapidjson::Value& customer = objValue["customer"];
order.customerInfo.name=customer.GetString();
rapidjson::Value& phone = objValue["phone"];
order.customerInfo.phone=phone.GetString();
rapidjson::Value& address = objValue["address"];
order.customerInfo.address=address.GetString();
//商品列表
rapidjson::Value& products_array = objValue["products"];
if(products_array.IsArray()){
for(unsigned int j=0;j<products_array.Size();j++){
productAttr proAttr;
rapidjson::Value& objProductValue = products_array[j];
rapidjson::Value& name = objValue["name"];
proAttr.pro.name=name.GetString();
rapidjson::Value& pid = objValue["pid"];
proAttr.pro.sku=pid.GetString();
rapidjson::Value& price = objValue["price"];
proAttr.pro.price=price.GetInt();
rapidjson::Value& productAmount = objValue["productAmount"];
proAttr.pro.qty=atoi(productAmount.GetString());
order.vecProducts.push_back(proAttr);
}
}
vecOrders.push_back(order);
}
}
}
}
}
std::string JsonModule::convertToOrderJson(orderObj &obj)
......@@ -543,3 +646,20 @@ std::string JsonModule::convertToStockWarnJson(stockWarnObj &obj)
return buffer.GetString();
}
std::string JsonModule::_getDeliveryTypeString(int type)
{
std::string rlt;
switch(type)
{
case 0:
rlt=DELIVERY_TYPE_SELF;
break;
case 1:
rlt=DELIVERY_TYPE_PLATFORM;
break;
default:
break;
}
return rlt;
}
\ No newline at end of file
......@@ -2,6 +2,7 @@
#define JSON_MODULE_H
#include "../base/CommonStruct.h"
#include "../base/BaseDefine.h"
//std::string GetTestJson(const int statuscode,const char* msg,const char* orderid);
//void parseJson(const char* json);
......@@ -11,7 +12,7 @@ public:
JsonModule();
~JsonModule();
orderObj convertToOrderObj(const char* json);
void convertToOrderObj(IN const char* json,OUT std::vector<orderObj> &vecOrders);
std::string convertToOrderJson(orderObj &obj);
orderStatusObj convertToOrderStatusObj(const char* json);
......@@ -22,6 +23,9 @@ public:
stockWarnObj convertToStockWarnObj(const char* json);
std::string convertToStockWarnJson(stockWarnObj &obj);
private:
std::string _getDeliveryTypeString(int type);
};
#endif
......@@ -54,6 +54,13 @@ int main()
// parseJson(json.data());
JsonModule jsonMod;
orderObj obj;
productAttr product;
product.pro.source="123";
productSpec spec;
spec.name="只";
product.vecSpec.push_back(spec);
obj.vecProducts.push_back(product);
std::string orderInfo = jsonMod.convertToOrderJson(obj);
LOG(INFO)<<"订单信息转换成JSON:"<<orderInfo.data();
//------------end---------------
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment