日期:2014-05-16  浏览次数:20503 次

json的简单使用
{
"tables" : {
"baseTable" : "CRM_R_HYDJZB",
"fk" : "ZRS",
"order" : "HYJB,RS",
"pk" : "SHOP_NAME",
"valuefield" : "SHOP_NAME"
},
"where" : [
{
"andor" : "or",
"fieldType" : "NUMBER",
"leftBracket" : "(",
"leftField" : "SHOP_ID",
"leftTable" : "CRM_R_HYDJZB",
"operator" : "=",
"param" : "e",
"paramType" : "FIELD",
"rightBracket" : ")",
"rightTableName" : ")"
}
]
}

?

首先是读入

unicodestring Jsonstr

Value jsonDoc;
Json::Reader reader;
reader.parse(AnsiString(Jsonstr).c_str(), jsonDoc);
Value & where = jsonDoc["where"];
ShowWhere(where);

下面是在cxtreelist里显示

?

void ShowWhere(Value & where) {
if (where.isNull()) {
return;
}
int count = where.size();
for (int i = 0; i < count; i++) {
Value & expression = where[i];
TcxTreeListNode * node = this->cxTreeListWhere->Add();
node->Texts[0] = expression["leftBracket"].asCString();
node->Texts[1] = expression["leftTable"].asCString();
node->Texts[2] = expression["leftField"].asCString();
node->Texts[3] = expression["fieldType"].asCString();
node->Texts[4] = expression["operator"].asCString();
node->Texts[5] = expression["paramType"].asCString();
node->Texts[6] = expression["rightTableName"].asCString();
node->Texts[7] = expression["param"].asCString();
node->Values[8] = expression["andor"].asString() == "and";
node->Texts[9] = expression["rightBracket"].asCString();
}
}

?

下面是写入

?

Value jsonDoc;
string result="";
int i=0;
if (cxTreeListWhere->AbsoluteVisibleCount!=0){
Value where;
SetWhere(where);
jsonDoc["where"] = where;
i++;
}
if (i>0){
Json::StyledWriter writer;
result = writer.write(jsonDoc);
TFileStream * file = new TFileStream("d:\\sessionset.txt",
fmOpenWrite | fmCreate);
file->Write(result.c_str(), result.length());
delete file;
}
output=(result).c_str();

?

?

读取cxtreelist里的值

?

void SetWhere(Json::Value & where) {
TcxTreeListNode *node = this->cxTreeListWhere->Root->getFirstChild();
while (node) {
Value expression;
expression["leftBracket"] = AnsiString(node->Texts[0]).c_str();
expression["leftTable"] = AnsiString(node->Texts[1]).c_str();
expression["leftField"] = AnsiString(node->Texts[2]).c_str();
expression["fieldType"] = AnsiString(node->Texts[3]).c_str();
expression["operator"] = AnsiString(node->Texts[4]).c_str();
expression["paramType"] = AnsiString(node->Texts[5]).c_str();
expression["rightTableName"] = AnsiString(node->Texts[6]).c_str();
expression["param"] = AnsiString(node->Texts[7]).c_str();

expression["andor"] = node->Values[8].vt != VT_NULL && node->Values[8]
.operator bool() ? "and" : "or";
expression["rightBracket"] = AnsiString(node->Texts[9]).c_str();

where.append(expression);
node = node->getNextSibling();
}
}