0

http://api.steampowered.com/IPlayerService/GetRecentlyPlayedGames/v0001/?key=C1C2692961278F5A77124973E0ACA799&steamid=76561198170248415&format=json

^^ That is the JSon URL, I just want it to output basic things for now I.E "playtime_2weeks": 5398, "playtime_forever": 47551,

those ints '5398' and '47551'

This is my class thus far

package org.zach.csgo.record;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.Reader;
import java.net.URL;
import java.nio.charset.Charset;

import org.json.JSONException;
import org.json.JSONObject;

public class JsonReader {
    public static String key = "C1C2692961278F5A77124973E0ACA799";
  private static String readAll(Reader rd) throws IOException {
    StringBuilder sb = new StringBuilder();
    int cp;
    while ((cp = rd.read()) != -1) {
      sb.append((char) cp);
    }
    return sb.toString();
  }

  public static JSONObject readJsonFromUrl(String url) throws IOException, JSONException {
    InputStream is = new URL(url).openStream();
    try {
      BufferedReader rd = new BufferedReader(new InputStreamReader(is, Charset.forName("UTF-8")));
      String jsonText = readAll(rd);
      JSONObject json = new JSONObject(jsonText);
      return json;
    } finally {
      is.close();
    }
  }

  public static void main(String[] args) throws IOException, JSONException {
    JSONObject json = readJsonFromUrl("http://api.steampowered.com/IPlayerService/GetRecentlyPlayedGames/v0001/?key="+key+"&steamid=76561198170248415&format=json");
    System.out.println(json.toString());
//What do I put here to just get info :P
  }
}

I believe this is enough information I apologize if it's not. I made this mistake the first time I asked a question on these forums!

arodriguezdonaire
  • 5,396
  • 1
  • 26
  • 50
Eggspurt
  • 63
  • 1
  • 9

2 Answers2

0

Look at this working example but most important look at this site http://www.json.org/ and try to understand structure of JSON, it isn't complicated but it takes some time to 'feel' JSON.

public static void main(String[] args) throws IOException, JSONException {
    JSONObject json = readJsonFromUrl("http://api.steampowered.com/IPlayerService/GetRecentlyPlayedGames/v0001/?key="+key+"&steamid=76561198170248415&format=json");
    System.out.println(json.toString());

    JSONObject response = json.getJSONObject("response"); //you get response, it's Json Object 
    int totalCount = response.getInt("total_count");   //total_count, it's name/value pair total_count/5
    JSONArray games = response.getJSONArray("games");  //games, it's Json Array

    System.out.println("Games total: " + totalCount);

    for(int i = 0; i < games.length(); i++){  //we're iterating through the array of Json Objects
        JSONObject game = games.getJSONObject(i); //every game is Json Object

        int appId = game.getInt("appid");   //in every game we have 5 key/value pairs appid/730, etc...
        String name = game.getString("name");  
        int playtime2weeks = game.getInt("playtime_2weeks");
        int playtimeForever = game.getInt("playtime_forever");
        String imgIconUrl = game.getString("img_icon_url");
        String imgLogoUrl = game.getString("img_logo_url");

        System.out.println("appid: " + appId + " name: " + name + " playtime2weeks: " + playtime2weeks + " playtime_forever: " + playtimeForever);
    }
Anatoly
  • 5,056
  • 9
  • 62
  • 136
-1

http://www.json.org/javadoc/org/json/JSONObject.html

That is a link to the JSONObject API. It specifies several methods you could use to get data from your object. Look for the ones that start with get*

Troncoso
  • 2,343
  • 3
  • 33
  • 52