Question

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
        }
    ]

}
}
Was it helpful?

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);`

OTHER TIPS

After reading your Question .It seems you have 2 problem

To generate JSON online for that I would like to suggest you to Visit this Link . this will generate the required JSON what you want.

for parsing the json see the below codes suppose you have put this JSON response in String Variable result

            String result = "yourresponse";

            try {
                JSONObject object = new JSONObject(result);
                JSONObject object1 = object.getJSONObject("response");
                JSONArray arr = object1.getJSONArray("Players");
                for (int i = 0; i < arr.length(); i++) 
                {
                      JSONObject object3 = arr.getJSONObject(i);
                      String steamId = object3.getString("steamid");
                      String communityvisibilitystate = object3
                        .getString("communityvisibilitystate");
                }

            } catch (JSONException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }

You can do sthing like this:

Object obj = parser.parse(new FileReader("c:\\test.json"));

JSONObject jsonObject = (JSONObject) obj;

String steamid= (String) jsonObject.get("steamid");
System.out.println(steamid);

But i recommend using gson. It is much easier, and well documented.

Create a class Result with the exact same structure as your json response (see below structure, for example)

class Result {
    class Response {
        list of (<class Players> {
             String steamid
             String communityvisibilitystate
             String profilestate
             String personaname
             String lastlogoff
             String profileurl
             String avatar
             String avatarmedium
             String avatarfull
             String personastate
             String primaryclanid
             String timecreated
             })
       }
   }
}

Then use gson to parse your json like this.

Result pareseJson = new Gson().fromJson(jsonString, Result.class);
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top