void CTestJSON::parseJsonStr( const std::wstring& strJsonStr ) { JSONValue* jsInput = JSON::Parse(strJsonStr.c_str()); if (jsInput == NULL || !jsInput->IsObject()) { return; } JSONObject::const_iterator itResult = jsInput->AsObject().find(L"result"); if (itResult != jsInput->AsObject().end()) { std::wstring strResult = itResult->second->AsString(); std::wcout << L"result" << L":" << strResult << std::endl; } JSONObject::const_iterator itLove = jsInput->AsObject().find(L"Love"); if (itLove != jsInput->AsObject().end()) { std::wstring strResult = itLove->second->AsString(); std::wcout << L"Love" << L":" << strResult << std::endl; } JSONArray jsArray; JSONObject::const_iterator itContents = jsInput->AsObject().find(L"contents"); if (itContents != jsInput->AsObject().end() && itContents->second != NULL && itContents->second->IsArray()) { jsArray = itContents->second->AsArray(); } std::wcout << "[" << std::endl; JSONArray::iterator it = jsArray.begin(); JSONArray::iterator itEnd = jsArray.end(); for (; it != itEnd; ++it) { JSONValue* jsValue = *it; if (jsValue->IsObject()) { jsValue->AsObject(); JSONObject::const_iterator itObj = jsValue->AsObject().begin(); JSONObject::const_iterator itObjEnd = jsValue->AsObject().end(); for (; itObj != itObjEnd; ++itObj) { std::wstring strValue = itObj->second->AsString(); std::wcout << L"{" << itObj->first << L":" << strValue << L"}" << std::endl; } } else if (jsValue->IsString()) { std::wstring strValue = jsValue->AsString(); std::wcout << strValue << std::endl; } else if (jsValue->IsNumber()) { double dValue = jsValue->AsNumber(); std::wcout << dValue << std::endl; } //... } std::wcout << "]" << std::endl; }
INT Configuration::getInt(const wstring& field) { JSONValue *c = json[field]; if (c == NULL || c->IsNumber() == FALSE) { throw new exception(); } return static_cast<INT>(c->AsNumber()); }
INT Configuration::getIntFromArray(const wstring& arrayName, const wstring& field) { JSONObject::const_iterator iter; JSONValue *c = json[arrayName]; if (c == NULL || c->IsObject() == FALSE) { throw new exception(); } JSONObject arr = c->AsObject(); JSONValue *val = arr[field]; if (val == NULL || val->IsNumber() == FALSE) { throw new exception(); } return static_cast<INT>(val->AsNumber()); }