How to iterate though each character of a string from a json file with Boost?

Nov 19, 2015 at 7:19pm
I'm trying to load up a game level using a pseudomap that I have stored in a json file, but I don't know the syntax on how to iterate through every character of the string. Here is an example of the json pseudomap:
1
2
3
4
5
6
7
{
  levels{
    level1{
      "map": "wwwww...................\nww...................www",
    }
  }
}


So everywhere there is a 'w' I want to place a wall. The \n is a placeholder for a line break ina standard .txt file

Here is my attempt at this, but it doesn't compile
1
2
3
4
5
6
7
8
boost::property_tree::ptree pt;
boost::property_tree::read_json("levels/level" + std::to_string(levelNumber) + ".json", pt);
std::string pseudomap("levels.level1.map");
for(unsigned int i = 0; i < pt.get_child(pseudomap).size(); i++){
	if(pt.get_child(pseudomap)[i] == "w"){ //I want to treat pt like a vector of chars that I can iterate through
		AddWall();
	}
}

What doesn't compile is if(pt.get_child(pseudomap)[i] because I don't know how to access a specific element of the current string in a boost property tree. Can anybody help me solve this?
Last edited on Nov 19, 2015 at 7:20pm
Nov 19, 2015 at 7:49pm
I used property_tree before and I don't have my code with me, but maybe this will help:

Do you really mean to use get_child() on map? It is just a string, so I would have thought that something like
pt.get<std::string>("levels.level1.map")
would get you a string that you can then do what you like with.
Nov 20, 2015 at 1:43pm
Hmm okay, what about accessing elements in an array in a .json file?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
{
	"levels":
	{
		"level1": {
			"map": "wwwww...................\nww...................www",
			"spawnpoints": [
				{"x": 20, "y": 30}
			],
			"itemSpawns": [
				{"itemType": "powerUp", "x": 100, "y": 30}
			]
		}
	}
}


How do I access "spawnpoints.x"?
pt.get<float>("levels.level1.spawnpoints.x" does not work
Nov 20, 2015 at 2:02pm
I'm no expert in boost property trees, but you've made "spawnpoints" an array (because of the square brackets [...]) containing a single element. So you'd need to specifically access the first element of that array.

Or, presumably, you really want to iterate over each element of that array in turn, if you're generalizing to more than one spawn point.
Last edited on Nov 20, 2015 at 2:04pm
Nov 20, 2015 at 4:02pm
Guessing again, but maybe something like:
1
2
3
4
for(boost::property_tree::ptree::value_type &v : pt.get_child("levels.level1.spawnpoints")) {
    // use v, which IIRC is another property tree representing the subtree
    // in your case that would be a tree with x and y children, I think
}
Nov 20, 2015 at 5:15pm
for(boost::property_tree::ptree::value_type &v : pt.get_child("levels.level1.spawnpoints"))

This for loop doesn't iterate at all. The code skips right over it

for(boost::property_tree::ptree::value_type &v : pt.get_child("levels.level1"))
However this foor loop does iterate


Okay so that code does work. In order to pause the program to check, I was placing a breakpoint on int i; when I needed to type int i = 0;

Here is the full loop
1
2
3
for(boost::property_tree::ptree::value_type &v : pt.get_child("levels.level1.spawnpoints")){
			spawnPoint.x = v.second.get<float>("x");
		}


Thanks guys
Last edited on Nov 20, 2015 at 5:27pm
Topic archived. No new replies allowed.