- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
谁能看出为什么我的 s.getSymbol()
错误在此代码中返回 null
?
json 可以很好地打印到终端,并且在创建“引用”时没有运行时错误。
然而,试图访问它的成员会抛出一个空指针。
我是否需要在我的 POJO 中包含 JSON 的所有节点?例如,如果我只想要 Symbol,我可以只拥有那个数据成员,只要它是 setter 和 getter 吗?
谢谢
Retorfit Interface
public class RetrofitInterface {
private static StockApiInterface sStockService;
public static StockApiInterface getStockApiClient() {
if (sStockService == null) {
RestAdapter restAdapter = new RestAdapter.Builder()
.setEndpoint("http://query.yahooapis.com/v1/public")
.setLogLevel(RestAdapter.LogLevel.FULL)
.build();
sStockService = restAdapter.create(StockApiInterface.class);
}
return sStockService;
}
public interface StockApiInterface {
@GET("/yql?q=select%20*%20from%20yahoo.finance.quotes%20where%20symbol%20in%20
(\"AIB.IR\")%0A%09%09&format=json&diagnostics=false&env=http%3A%2F%2Fdatatables.org%2Falltables.env&callback=")
quote listQuotes();
}
主要 Activity
quote q = RetrofitInterface.getStockApiClient().listQuotes();
if(q.getSymbol()== null){
System.out.println("Null");
}
JSON
{"query":{"count":1,"created":"2014-11-17T11:06:28Z","lang":"en-US","results":{"quote":{"symbol":"AIB.IR","Ask":"0.105","AverageDailyVolume":"812179","Bid":"0.103","AskRealtime":"0.105","BidRealtime":"0.103","BookValue":"0.00","Change_PercentChange":"0.00 - 0.00%","Change":"0.00","Commission":null,"Currency":"EUR","ChangeRealtime":"0.00","AfterHoursChangeRealtime":"N/A - N/A","DividendShare":"0.00","LastTradeDate":"11/17/2014","TradeDate":null,"EarningsShare":"0.00","ErrorIndicationreturnedforsymbolchangedinvalid":null,"EPSEstimateCurrentYear":"0.00","EPSEstimateNextYear":"0.00","EPSEstimateNextQuarter":"0.00","DaysLow":"0.101","DaysHigh":"0.108","YearLow":"0.075","YearHigh":"0.166","HoldingsGainPercent":"- - -","AnnualizedGain":null,"HoldingsGain":null,"HoldingsGainPercentRealtime":"N/A - N/A","HoldingsGainRealtime":null,"MoreInfo":"n","OrderBookRealtime":null,"MarketCapitalization":null,"MarketCapRealtime":null,"EBITDA":"0","ChangeFromYearLow":"+0.028","PercentChangeFromYearLow":"+37.33%","LastTradeRealtimeWithTime":"N/A - <b><img src=http://eur.i1.yimg.com/eur.yimg.com/i/eu/f/e.gif width=11 height=13 border=0>0.103</b>","ChangePercentRealtime":"N/A - 0.00%","ChangeFromYearHigh":"-0.063","PercebtChangeFromYearHigh":"-37.95%","LastTradeWithTime":"5:40am - <b>0.103</b>","LastTradePriceOnly":"0.103","HighLimit":null,"LowLimit":null,"DaysRange":"0.101 - 0.108","DaysRangeRealtime":"N/A - N/A","FiftydayMovingAverage":"0.00","TwoHundreddayMovingAverage":"0.00","ChangeFromTwoHundreddayMovingAverage":null,"PercentChangeFromTwoHundreddayMovingAverage":null,"ChangeFromFiftydayMovingAverage":null,"PercentChangeFromFiftydayMovingAverage":null,"Name":"ALLIED IRISH BANK","Notes":null,"Open":"0.101","PreviousClose":"0.103","PricePaid":null,"ChangeinPercent":"0.00%","PriceSales":null,"PriceBook":null,"ExDividendDate":null,"PERatio":null,"DividendPayDate":null,"PERatioRealtime":null,"PEGRatio":null,"PriceEPSEstimateCurrentYear":null,"PriceEPSEstimateNextYear":null,"Symbol":"AIB.IR","SharesOwned":null,"ShortRatio":null,"LastTradeTime":"5:40am","TickerTrend":" +++-+- ","OneyrTargetPrice":null,"Volume":"228977","HoldingsValue":null,"HoldingsValueRealtime":null,"YearRange":"0.075 - 0.166","DaysValueChange":"- - 0.00%","DaysValueChangeRealtime":"N/A - N/A","StockExchange":"Irish","DividendYield":null,"PercentChange":"0.00%"}}}}
POJO
public class quote {
@Expose
private String symbol;
@Expose
private String Ask;
@Expose
private String AverageDailyVolume;
@Expose
private String Bid;
@Expose
private String AskRealtime;
.....
//includes all elements and setters and getter
最佳答案
http://www.jsonschema2pojo.org/生成了以下(缩短的)POJOS,应该是可解析的:
public class ResultWrapper {
Query query;
(...)
}
public class Query {
private Integer count;
private String created;
private String lang;
private Results results;
(...)
}
public class Results {
// you might wanna make this an array in your json and a List here
private Quote quote;
(...)
}
public class Quote {
private String symbol;
private String Ask;
private String AverageDailyVolume;
private String Bid;
private String AskRealtime;
private String BidRealtime;
private String BookValue;
private String ChangePercentChange;
private String Change;
private Object Commission;
private String Currency;
private String ChangeRealtime;
private String AfterHoursChangeRealtime;
private String DividendShare;
private String LastTradeDate;
private Object TradeDate;
private String EarningsShare;
private Object ErrorIndicationreturnedforsymbolchangedinvalid;
private String EPSEstimateCurrentYear;
private String EPSEstimateNextYear;
private String EPSEstimateNextQuarter;
private String DaysLow;
private String DaysHigh;
private String YearLow;
private String YearHigh;
private String HoldingsGainPercent;
private Object AnnualizedGain;
private Object HoldingsGain;
private String HoldingsGainPercentRealtime;
private Object HoldingsGainRealtime;
private String MoreInfo;
private Object OrderBookRealtime;
private Object MarketCapitalization;
private Object MarketCapRealtime;
private String EBITDA;
private String ChangeFromYearLow;
private String PercentChangeFromYearLow;
private String LastTradeRealtimeWithTime;
private String ChangePercentRealtime;
private String ChangeFromYearHigh;
private String PercebtChangeFromYearHigh;
private String LastTradeWithTime;
private String LastTradePriceOnly;
private Object HighLimit;
private Object LowLimit;
private String DaysRange;
private String DaysRangeRealtime;
private String FiftydayMovingAverage;
private String TwoHundreddayMovingAverage;
private Object ChangeFromTwoHundreddayMovingAverage;
private Object PercentChangeFromTwoHundreddayMovingAverage;
private Object ChangeFromFiftydayMovingAverage;
private Object PercentChangeFromFiftydayMovingAverage;
private String Name;
private Object Notes;
private String Open;
private String PreviousClose;
private Object PricePaid;
private String ChangeinPercent;
private Object PriceSales;
private Object PriceBook;
private Object ExDividendDate;
private Object PERatio;
private Object DividendPayDate;
private Object PERatioRealtime;
private Object PEGRatio;
private Object PriceEPSEstimateCurrentYear;
private Object PriceEPSEstimateNextYear;
private String Symbol;
private Object SharesOwned;
private Object ShortRatio;
private String LastTradeTime;
private String TickerTrend;
private Object OneyrTargetPrice;
private String Volume;
private Object HoldingsValue;
private Object HoldingsValueRealtime;
private String YearRange;
private String DaysValueChange;
private String DaysValueChangeRealtime;
private String StockExchange;
private Object DividendYield;
private String PercentChange;
(...)
}
然后修改你的retrofit界面为
public interface StockApiInterface {
@GET("/yql?q=select%20*%20from%20yahoo.finance.quotes%20where%20symbol%20in%20(\"AIB.IR\")%0A%09%09&format=json&diagnostics=false&env=http%3A%2F%2Fdatatables.org%2Falltables.env&callback=")
ResultWrapper listQuotes();
}
您现在可以通过以下方式访问报价:
ResultWrapper result = RetrofitInterface.getStockApiClient().listQuotes();
Quote q = q.getQuery().getResults().getQuote();
关于android - 改造POJO为空,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26972527/
我刚刚发现了 Camel,它似乎正是我所需要的。 我有几个知道如何处理特定输入数据的构建 block ,所以我想创建一个非常简单的 GUI 供用户选择构建 block ,只需将它们一个接一个地链接起来
根据Convert Spark DataFrame to Pojo Object的回复我了解到 Dataframe是 Dataset 的别名. 我目前计算了 JavaPairRDD哪里CityStat
我使用的是jackson,场景是: 在客户端 (Android) 中生成对 Web 服务的 URL 调用 Web 服务以 json 形式返回“complex pojo”(具有与其他 pojo 相同的类
我需要将文档存储到 Elasticsearch 索引中,因此我定义了一个映射。我需要从我的 java 客户端向它提供看起来像 Compony 类的 pojo。它有很多重复的值。我可以使用对象组合模式来
所以我正在开发一个聊天应用程序。有一部分我感兴趣的是一次从数据库中检索 3 个实体。有一个 serverDto 实体和 accountDto 实体。每个服务器都有很多账户,账户里有一个字段叫做acti
我有一个 Java 中的 POJO 列表,我需要将其转换为另一种类型的 POJO 列表。两个 POJO 都有两个字符串字段。除了迭代原始列表并将新项目添加到结果列表之外,是否有更好的方法来执行此操作?
我正在开发一个 API 来访问存储在系统中的数据。该系统包含人员、约会和与这些约会相关的程序等内容。我的应用程序将严格为只读。 我正在使用带 RowMapper 的 Spring 来构建对象,例如“P
希望有人能帮助我。我有一个具有以下结构的 POJO: public class Invoice{ private String docNum; private String customer; pri
引用以下链接,我在运行时创建了类,http://blog.javaforge.net/post/31913732423/howto-create-java-pojo-at-runtime-with-j
所以我遇到了这个教程,将 POJO 序列化为 json,然后将 json 文件反序列化回 POJO。 http://www.mkyong.com/java/how-to-convert-java-ob
我是 Java 的新手。我只是对序列化和反序列化感到困惑。所以,我很困惑我应该使用哪一个。 我正在寻找一个回合,发现 Boon、Jackson、GSON(我目前正在使用 GSON,但一些文章使用 Ja
我确信这个问题可能已经被问过几次了,但我不明白我应该使用什么查询。我想要做的是,将 POJO 传递给另一个参数可以是动态的 POJO 像下面的例子 Class DataPOJO{ privat
我有一个有多页的注册表。所以,我将一页映射到一个 pojo。最后,当我处理数据库时,我想将其作为单个 pojo 提交。任何简化此模型的想法。 谢谢:) 最佳答案 您可以创建一个包装器 POJO,它在主
希望有人能帮助我。我有一个具有以下结构的 POJO: public class Invoice{ private String docNum; private String customer; pri
要求: 将 json 文件中选定的字段收集到 POJO 中(字段名称不匹配) 更新 POJO 的其他非映射字段 转换回带有 POJO 字段名的 jsonNode 问题:步骤 1 和 2 没问题。当将
我的 Spring Boot 应用程序中有两个 Pojo Pojo 1:FeedData 代码(我有获取和设置,只是没有在这里显示): package com.cms.tb.model; import
我们有两个 Pojo 文件。 Person { String name; int age; String address; String phoneNo; boolean isMa
我有一个数据库调用,如果没有任何条件匹配,则可能返回 null。如果有记录匹配,则结果是一个包含嵌入对象列表的 Pojo。我想将该 Pojo 转换为其嵌入对象 id 的列表。 Foo.class 有
我正在尝试使用流将 pojo 映射到 pojo 上的属性公开的一组项目。我意识到这还不清楚,所以我将展示我是如何在没有流的情况下完成的。 我有产品类别的枚举,产品的枚举和具有此类产品的商店列表 pub
我们在 Apache2 网络服务器后面的 Tomcat 6 中运行的 AXIS2 v1.5.2 中实现了一个 POJO 网络服务。 出于演示目的,我将重点介绍 validateUser 消息和相应的方
我是一名优秀的程序员,十分优秀!