当前位置: 首页>>代码示例>>C++>>正文


C++ JsonArray::Add方法代码示例

本文整理汇总了C++中JsonArray::Add方法的典型用法代码示例。如果您正苦于以下问题:C++ JsonArray::Add方法的具体用法?C++ JsonArray::Add怎么用?C++ JsonArray::Add使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在JsonArray的用法示例。


在下文中一共展示了JsonArray::Add方法的2个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C++代码示例。

示例1: main

int main(int argc, char *argv[0]) {
  	// Declare a string variable to store a filename
  	std::string aFile;
	string		buf;	
  	// Declare a variable to read in a JSON file
  	ifstream inFile;

  	// Check for a command line argument
  	if (argc != 2) {
    	cout << "usage: " << argv[0] << " <filename>\n";
  		} else {
    	// store the command argument as the filename
    	aFile = argv[1];
  		}

  	// Read in a .json file
  	inFile.open(aFile);

  	// Check that the file name was opened successfully
  	if (inFile.fail( )) {
    	// Print an error message
    	cout << "Error opening file.\n\n";
    	exit(1);
  		} else {
    	// The file was successfully opened
    	cout << "File opened successfully.\n\n";
  		}
    // regular expressions for matches      
	regex	openBraceRE(R"(\s*[\{\[])");
    regex   closeBraceRE(R"(\s*[\}\]])");
	regex 	arrayRE(R"(\[(.+)\])") ;
    regex   emptyRE(R"(^\s*$)");
	regex   jLineRE(R"(\x22(\w+)\x22\s*: (.*),?)");
	regex   jIntRE(R"(\s*(\d+),?)");	
    regex   jStrRE(R"(\s*\x22(\w+)\x22\s*,?)");
    regex   jBoolorNullRE(R"(\s*(\w+),?)");

    // some matches and strings for thos matches
    smatch	fileTypeMatch;
    smatch  jLineMatch;
    smatch  jValMatch;
    
    string  jsonID;
    string  jsonVal;
    bool    openBraceNotFound = true;
    // temp variables for valid types found
    int     jInt;
    string  jStr;
    bool    jBool;
    string  jNull = "null";    

    // json objects 
    JsonObject* 	jObject = new JsonObject();      
    JsonArray *     jArray = new JsonArray();

    do {
        getline(inFile, buf);
        // check for empty line 
        if(regex_search(buf, jLineMatch, emptyRE)) {
            cout << "empty line...\n";
            continue;
            }
        // check for open brace
        if(regex_search(buf, jLineMatch, openBraceRE)) {    
            openBraceNotFound = false;  
            continue;            
            }
        // if the first character is not a brace display error and exit
        if (openBraceNotFound) {
            cout <<"ERROR expected {...\n";
            exit(1);
            }
        // check for close brace    
        if(regex_search(buf, jLineMatch, closeBraceRE)) {
            break; 
            }
        // if valid line found, save id and value
        if(regex_search(buf, jLineMatch, jLineRE))
            {
            jsonID = jLineMatch.str(1);
            jsonVal = jLineMatch.str(2);
            }
        // if valid line not found, put an error and exit
        else {
            cout << "\nERROR: Not a valid line! \n";
            exit(1);           
            }
        // if valid array found add too the object
        if (regex_search(jsonVal, jValMatch, arrayRE)) {
            cout << "Array matched!\n";
            jArray->Add(new JsonString(jLineMatch.str(1)));
            jObject->Add(jsonID, jArray);
            }
        // if valid integer found add it too the object    
        else if (regex_search(jsonVal, jValMatch, jIntRE)) {
            jInt = stoi(jsonVal);

            cout << "Valid Int " << jInt << " matched! \n";
            jObject->Add(jsonID, new JsonNumber(jInt));
            }    
//.........这里部分代码省略.........
开发者ID:skrapmi,项目名称:CPSC-323,代码行数:101,代码来源:main2.cpp

示例2: BuildIndexForUser

void UsersPanel::BuildIndexForUser(JsonArray * friends) {
	/*
	String letter, prevLetter = L"";
	int currentGroup = 0;

	JsonArray * array = new JsonArray();
	array->Construct();

	for (int i=0; i<friends->GetCount(); i++) {
		JsonObject *user;
		JsonParseUtils::GetObject(friends, i, user);

		String name;
		JsonParseUtils::GetString(*user, L"first_name", name);

		name.SubString(0, 1, letter);

		if (letter != prevLetter) {
			_fastScrollIndex.Append(letter);
			_pCurrentModel->AddUserGroup(prevLetter, array);

			if (prevLetter.GetLength() > 0) {
				AppLog("Adding %ls to %d item", prevLetter.GetPointer(), currentGroup);
				_pLetterNav->Add(new String(prevLetter), new Integer(currentGroup));
				currentGroup++;
			}

			prevLetter = letter;
			delete array;
			array = new JsonArray();
			array->Construct();
		} else {
			array->Add(user);
		}

	}*/

	HashMap *letterMap = new HashMap();
	letterMap->Construct();

	for(int i = 0; i < friends->GetCount(); i++) {
		JsonObject *user;
		JsonParseUtils::GetObject(friends, i, user);

		String name;
		JsonParseUtils::GetString(*user, L"first_name", name);

		String letter;
		name.SubString(0, 1, letter);

		JsonArray *letterArray;
		if(!letterMap->ContainsKey(letter)) {
			letterArray = new JsonArray();
			letterArray->Construct();
			letterMap->Add(new String(letter), letterArray);
		} else {
			letterArray = static_cast<JsonArray*>(letterMap->GetValue(letter));
		}

		letterArray->Add(user);
	}

	IList *letterList = letterMap->GetKeysN();
	StringComparer comparer;
	letterList->Sort(comparer);
	for(int i = 0; i < letterList->GetCount(); i++) {
		String *letter = static_cast<String *>(letterList->GetAt(i));
		_fastScrollIndex.Append(*letter);
		_pCurrentModel->AddUserGroup(*letter, static_cast<JsonArray *>(letterMap->GetValue(*letter)));
		_pLetterNav->Add(new String(*letter), new Integer(i));
	}

	delete letterList;
	delete letterMap;
}
开发者ID:igorglotov,项目名称:tizen-vk-client,代码行数:75,代码来源:UsersPanel.cpp


注:本文中的JsonArray::Add方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。