从带有Java的JSON文件中获取信息

时间:2016-01-04 23:03:13

标签: java json

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

^^这是JSon URL,我只想让它现在输出基本的东西I.E" playtime_2weeks":5398, " playtime_forever":47551,

那些整数' 5398'和' 47551'

到目前为止,这是我的班级

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

我相信这是足够的信息我道歉,如果不是。我第一次在这些论坛上提问时犯了这个错误!

2 个答案:

答案 0 :(得分:0)

看看这个工作示例,但最重要的是看看这个网站http://www.json.org/并尝试理解JSON的结构,它并不复杂,但需要一些时间来“感觉”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);
    }

答案 1 :(得分:-1)

Express docs

这是JSONObject API的链接。它指定了几种可用于从对象获取数据的方法。寻找以 get *

开头的那些
相关问题