I've been making a Minecraft launcher. I have a long JSON file that contains all the libraries required to launch one version. A sample of this code:
{
"id": "1.6.2",
"time": "2013-08-06T14:00:00+03:00",
"releaseTime": "2013-07-05T16:09:02+03:00",
"type": "release",
"minecraftArguments": "--username ${auth_player_name} --session ${auth_session} --version ${version_name} --gameDir ${game_directory} --assetsDir ${game_assets}",
"libraries": [
{
"name": "net.sf.jopt-simple:jopt-simple:4.5"
},
{
"name": "com.paulscode:codecjorbis:20101023"
},
So you can see that there is an array called libraries. I can query the values in "name" using a foreach quite fine, but sometimes this occurs in the json:
{
"name": "org.lwjgl.lwjgl:lwjgl:2.9.0",
"rules": [
{
"action": "allow"
},
{
"action": "disallow",
"os": {
"name": "osx",
"version": "^10\\.5\\.\\d$"
}
}
]
},
So as you can see, there is an array inside called rules. I need to find the value of name inside os inside rules. Anything I've tried came up with an exception and wouldn't work. This is my code at the moment for parsing the code:
foreach (JToken lib in profilejsono["libraries"])
{
if ((lib["rules"][1]["os"].ToString() == "osx") || (lib["rules"][1]["os"].ToString() == "linux"))
{
availableLibs.Add(lib["name"].ToString());
}
}
The point of the code is not to add the library if it's for OSX or Linux (I'll add that functionality later). How do I do this?
["os"]value. Theforeachloop is returning eachJtokenbut in fact only one item will return value forlib["rules"][1]["os"]otherwise you getnullbecause this item just don't have the property"os".lib["rules"].count >= 2, because you want the second element which has index[1]then, check if it has"os"property and so on... I don't tell it's the best way, but just to get you on the right track..