Search code examples
javajsondecodejson-simple

java decode json help (using json-simple)


I know that asking for making something is bad but it's first time I'm stucked in java. Recently I began to learn java and for my small tool I need to read this json string.

Half a day I was searching and trying to make things work and founded json-simple.

But i cant do this. Please help.

For example just retrieve steamid and communityvisibilitystate to strings

String steamid = ...;
String communityvisibilitystate = ...;

and if you have better solution not using json-simple post it

{
"response": {
    "players": [
        {
            "steamid": "76561198777777777",
            "communityvisibilitystate": 3,
            "profilestate": 1,
            "personaname": "777777",
            "lastlogoff": 7777777777,
            "profileurl": "http://steamcommunity.com/profiles/76561198777777777/",
            "avatar": "http://media.steampowered.com/steamcommunity/public/images/avatars/16/777777777.jpg",
            "avatarmedium": "http://media.steampowered.com/steamcommunity/public/images/avatars/16/777777777_medium.jpg",
            "avatarfull": "http://media.steampowered.com/steamcommunity/public/images/avatars/16/777777777_full.jpg",
            "personastate": 0,
            "primaryclanid": "103582797777777777",
            "timecreated": 7777777777
        }
    ]

}
}

Solution

  • 1 : your complete response is a JSON OBJECT

    2 : if any element is written like

    "some key name " : { " some value " }
    

    this is a JSON Object

    3 : if any element is writen like

     "some key name " :  " some value " 
    

    this is value inside you json object which you can get by

    jsonObject.getString("key name")
    

    4 : if any element is writen like

    "some key name " : [ " some value " ]
    

    then this is a JSON Array and you have to take it in to a JSON ARRAY and then traverse its elements by

    jsonObject.getJSONARRAY("key name for JSON ARRAY IN RESPONSE ")
    

    and then you can traverse the elements of the JSON ARRAY by

    `jsonArrayObj.get(0);`