Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include "stdafx.h"
- #include <stdio.h>
- #include <map>
- #include <string>
- #include <iostream>
- #include <algorithm>
- using namespace std;
- map<string, string> GetArgMap(string text)
- {
- map<string, string> DataMap;
- size_t pos1 = 0;
- while (true)
- {
- pos1 = text.find('"', pos1);
- if(pos1 == string::npos)
- break;
- size_t pos2 = text.find('"', ++pos1);
- if(pos2 == string::npos)
- break;
- string Key = text.substr(pos1, pos2-pos1);
- if(Key.empty())
- break;
- pos1 = text.find(':', pos1);
- if(pos1 == string::npos)
- break;
- pos2 = text.find(',', ++pos1);
- if(pos2 == string::npos)
- {
- pos2 = text.find('}', pos1);
- if(pos2 == string::npos)
- break;
- }
- string Value = text.substr(pos1, pos2-pos1);
- if(Value.empty())
- break;
- Value.erase(remove(Value.begin(), Value.end(), '"'), Value.end());
- DataMap[Key] = Value;
- pos1 = pos2;
- }
- return DataMap;
- }
- int main()
- {
- string text = "{\"countryId\":17,\"maxMembers\":20,\"name\":\"Resolution\",\"goldValue\":200,\"totalDamage\":154295731,\"militaryUnitType\":\"Regular\"}"; // test text
- map<string, string> DataMap = GetArgMap(text); // get the arguments
- for(map<string, string>::iterator it = DataMap.begin(); it != DataMap.end(); ++it) // test print all arguments
- {
- cout << it->first << " " << it->second << endl;
- }
- cout << endl;
- cout << DataMap["name"] << endl; // test print name argument
- cin.get(); // pausing the console
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement