- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
json 类:
package json;
public class RankAPI {
private String hotStreak;
private String leagueName;
private String tier;
private String freshBlood;
private String playerOrTeamId;
private String leaguePoints;
private String inactive;
private String rank;
private String veteran;
private String queueType;
private String losses;
private String playerOrTeamName;
private String wins;
public String getHotStreak ()
{
return hotStreak;
}
public void setHotStreak (String hotStreak)
{
this.hotStreak = hotStreak;
}
public String getLeagueName ()
{
return leagueName;
}
public void setLeagueName (String leagueName)
{
this.leagueName = leagueName;
}
public String getTier ()
{
return tier;
}
public void setTier (String tier)
{
this.tier = tier;
}
public String getFreshBlood ()
{
return freshBlood;
}
public void setFreshBlood (String freshBlood)
{
this.freshBlood = freshBlood;
}
public String getPlayerOrTeamId ()
{
return playerOrTeamId;
}
public void setPlayerOrTeamId (String playerOrTeamId)
{
this.playerOrTeamId = playerOrTeamId;
}
public String getLeaguePoints ()
{
return leaguePoints;
}
public void setLeaguePoints (String leaguePoints)
{
this.leaguePoints = leaguePoints;
}
public String getInactive ()
{
return inactive;
}
public void setInactive (String inactive)
{
this.inactive = inactive;
}
public String getRank ()
{
return rank;
}
public void setRank (String rank)
{
this.rank = rank;
}
public String getVeteran ()
{
return veteran;
}
public void setVeteran (String veteran)
{
this.veteran = veteran;
}
public String getQueueType ()
{
return queueType;
}
public void setQueueType (String queueType)
{
this.queueType = queueType;
}
public String getLosses ()
{
return losses;
}
public void setLosses (String losses)
{
this.losses = losses;
}
public String getPlayerOrTeamName ()
{
return playerOrTeamName;
}
public void setPlayerOrTeamName (String playerOrTeamName)
{
this.playerOrTeamName = playerOrTeamName;
}
public String getWins ()
{
return wins;
}
public void setWins (String wins)
{
this.wins = wins;
}
@Override
public String toString()
{
return "ClassPojo [hotStreak = "+hotStreak+", leagueName = "+leagueName+", tier = "+tier+", freshBlood = "+freshBlood+", playerOrTeamId = "+playerOrTeamId+", leaguePoints = "+leaguePoints+", inactive = "+inactive+", rank = "+rank+", veteran = "+veteran+", queueType = "+queueType+", losses = "+losses+", playerOrTeamName = "+playerOrTeamName+", wins = "+wins+"]";
}
}
其他代码:
package com.rhidlor.leaguetool;
import json.SummonerAPI;
import json.RankAPI;
import com.google.gson.Gson;
import com.google.gson.annotations.SerializedName;
import java.io.IOException;
import java.net.URL;
import java.util.ResourceBundle;
import javafx.event.ActionEvent;
import javafx.fxml.FXML;
import javafx.fxml.Initializable;
import javafx.scene.control.Label;
import javafx.scene.control.TextField;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.Response;
public class FXMLController implements Initializable {
public static OkHttpClient client = new OkHttpClient();
public static Gson gson = new Gson();
public static SummonerAPI summonerAPI;
public static RankAPI rankAPI;
public static String summonerJSON = null;
public static String rankJSON = null;
public static String username;
public static String id;
public String key = "RGAPI-dd11b8d0-3178-4eb1-8dfa-0d5bf6fb1b24";
//https://na1.api.riotgames.com
@FXML private Label summonerLabel;
@FXML private Label rankLabel;
@FXML private Label winrateLabel;
@FXML private TextField usernameTextField;
@FXML
private void handleButtonAction(ActionEvent event) {
username = usernameTextField.getText();
try{
summonerJSON = getJSON("https://na1.api.riotgames.com/lol/summoner/v3/summoners/by-name/" + username + "?api_key=" + key);
}catch(Exception e) {
e.printStackTrace();
}
summonerAPI = gson.fromJson(summonerJSON, SummonerAPI.class);
id = summonerAPI.getId();
System.out.println("ID: " + id);
try{
rankJSON = getJSON("https://na1.api.riotgames.com/lol/league/v3/positions/by-summoner/" + id + "?api_key=" + key);
System.out.println("This worked");
}catch(Exception e) {
System.out.println("This failed");
e.getCause().printStackTrace();
}
rankAPI = gson.fromJson(rankJSON, RankAPI.class);
setValues(username);
}
public static String getJSON(String url) throws IOException {
Request request = new Request.Builder()
.url(url)
.build();
Response response = client.newCall(request).execute();
return response.body().string();
}
public void setValues(String username){
summonerLabel.setText(username + " Level: " + summonerAPI.getSummonerLevel());
//System.out.println(rankAPI.getWins());
//rankLabel.setText("Rank: " + rankAPI.getTier() + " " + rankAPI.getRank() + " LP: " + rankAPI.getLeaguePoints());
//int winrate = (parseInt(rankAPI.getWins()) / (parseInt(rankAPI.getWins()) + parseInt(rankAPI.getLosses()))) * 100;
//winrateLabel.setText("Winrate: " + winrate + "%");
}
@Override
public void initialize(URL url, ResourceBundle rb) {
// TODO
}
}
此行导致错误:
rankAPI = gson.fromJson(rankJSON, RankAPI.class);
错误:由以下原因引起:java.lang.IllegalStateException:预期为 BEGIN_OBJECT,但在第 1 行第 2 列路径 $ 处为 BEGIN_ARRAY
如果有人能帮助我,我将不胜感激。此代码的 SummonerAPI 部分工作完美,它的 json 类几乎与 RankAPI json 类相同。
我知道有些代码可能没有意义或看起来不合适,那是因为我试图对代码进行故障排除,但显然失败了。
最佳答案
看起来 JSON 字符串 ( rankJSON
) 是一个 JSON 文档数组,而不是单个 JSON 文档。
如果您记录该 JSON,您会发现它以 [
开头例如
[
{
...
}
]
您正在尝试将其反序列化为单个 RankAPI
,您应该将其反序列化为 List<RankAPI>
,例如;
List<RankAPI> r = gson.fromJson(rankJSON, new TypeToken<ArrayList<RankAPI>>(){}.getType());
这是验证此行为的测试用例:
@Test
public void twoWayTransform() {
Gson gson = new GsonBuilder().serializeNulls().create();
List<RankAPI> incomings = Arrays.asList(new RankAPI(), new RankAPI());
String json = gson.toJson(incomings);
// use TypeToken to inform Gson about the type of the elements in the generic list
List<RankAPI> fromJson = gson.fromJson(json, new TypeToken<ArrayList<RankAPI>>(){}.getType());
assertEquals(2, fromJson.size());
for (RankAPI incoming : incomings) {
// this will pass if RankAPI has an equals() method
assertTrue(fromJson.contains(incoming));
}
}
关于java - gson.fromJson 预期为 BEGIN_OBJECT,但实际为 BEGIN_ARRAY,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46580967/
我有两个维度 DimFlag 和 DimPNL 以及一个事实表 FactAmount 。我正在寻找:当 pnl 是 stat(Is Stat=1) 时:sum (Actual x FlagId)对于
我想对包含其部分内容的文本字段执行简单搜索,但我不知道从哪里开始。我基本上想要人们对“包含搜索”的期望。如果我在 issue 中搜索 345 ,我会想要这个结果: 123456 234567 3456
我在 VBE 的 C# 插件中有这段代码(强调“VBE”:它不是 MS-Office 插件): public abstract class HostApplicationBase : IHostApp
我有一个 ImageView,它显示来自资源的图像。ImageView 的宽度是固定的 (60dp)。高度设置为 wrap_content。调整图像大小以适合此宽度(节省宽高比 - 这很完美) 问题是
我正在建立一个网站,但遇到了一个问题:谷歌浏览器开发者工具中的背景以较低/较高的分辨率延伸。当我直接从手机打开网站时,背景不适合屏幕,只是“剪切”了背景。 这是网站:https://feargames
好吧,首先,这是 HTML 模板: ... ... ... ... 如您所见,页面位于标题下方,并且通过 JS 代码可见
我读到了 BK-trees (Burkhard-Keller-Trees) 几个月前,据说这是一种保存您想通过距离度量再次读取的内容的好方法。因此,在每种情况下,您都希望通过相似性检索某些内容。 然而
在 python 中,很容易根据字符数用空格填充字符串。例如: print "aaa".ljust(10) + "end" print "www".ljust(10) + "end" 输出是: aaa
我的问题不是特定于编程语言的,而是更通用的问题,以了解人们的思维方式。 通常在大型开发公司中,每项工作都有特定的角色,例如程序员和架构师。因此架构师的观点是拥有完美的架构师和解决方案设计,另一方面程序
我想将数据传递给 then 方法,但是当我通过给它 this.passedResolve 来执行此操作时,它会得到 undefined function Promises(callback){
我希望使用 Erlang/Elixir 在金融市场创建一个平台即服务。我将在金融市场提供 AWS lambda 风格的函数,但我计划向客户分发我自己的基于 ARM 的硬件终端(基于 Nvidia Je
已结束。此问题正在寻求书籍、工具、软件库等的推荐。它不满足Stack Overflow guidelines 。目前不接受答案。 我们不允许提出寻求书籍、工具、软件库等推荐的问题。您可以编辑问题,以便
我已经看到很多代码,这些代码使用Runnable的循环时间来实现某些计时器/超时。问题是,如果有人决定更改此Runnable的循环时间,则计时器将不正确。 例: #define FOO_TIMER_1
当我将 WPF DataGrid 的 ColumnHeaderHeight 设置为 Auto (double.NaN) 时,如何获取列标题的实际呈现高度? 我似乎无法在 DataGrid 类中找到该属
目前最实用的png修复方法是什么?轻量级,支持背景重复和背景位置。 最佳答案 IE7.JS在我看来: IE7.js is a JavaScript library to make Microsoft
我已经进行了长时间的搜索并尝试了常见的嫌疑人,但现在是寻求帮助的时候了。 我的 Android Activity 非常愉快地从 SQLite 加载 EditText、Spinner 和 CheckBo
因此,我在 MySQL 全文中创建精确搜索时遇到了一些困难。 在我的数据库中,我正在尝试查找标题中包含特定关键字的职位。 所以我可以尝试 WHERE MATCH(jobTitle) AGAINST (
我正在尝试将 JavaScript 包含到一个表单应用程序中,该应用程序从现场收集施工数据。我已经用谷歌搜索了这个废话,但我无法弄清楚将 html 元素保存在数组中是否合法(或者我的语法是否正确)。
我知道有六种方法可以获取 session.save_path 指令的值(phpinfo()、session_save_path()等),但当值为空字符串时(默认情况下为空字符串),实际路径可以是多个位
我知道 npm 库在安装时可以在分层树中安装同一库的多个版本,如下所示: a@0.1.0 -> b@1.0 -> c@2.0 -> b@2.0 在上面,版本0.1.0的包a被拉入
我是一名优秀的程序员,十分优秀!