我正在尝试从下面的JSON文件中解析"actualEPS“的第一个实例:
{"symbol":"AAPL","earnings":[{"actualEPS":2.34,"consensusEPS":2.17,"estimatedEPS":2.17,"announceTime":"AMC","numberOfEstimates":10,"EPSSurpriseDollar":0.17,"EPSReportDate":"2018-07-31","fiscalPeriod":"Q3 2018","fiscalEndDate":"2018-06-30","yearAgo":1.67,"yearAgoChangePercent":0.40119760479041916,"estimatedChangePercent":0.29940119760479045,"symbolId":11},{"actualEPS":2.73,"consensusEPS":2.69,...}
以下是目前的方法。我知道我正在从目标获取数据,因为我可以系统输出字符串"inputLine“并看到完整的文件。从这一点开始,我在解析时遇到了麻烦。我已经安装并导入了org.JSON库。
public static void getData(String s) throws Exception {
String urlBase = new String(theWebSiteImGettingDataFrom)
URL targetURL = new URL(urlBase);
URLConnection yc = targetURL.openConnection();
BufferedReader in = new BufferedReader(
new InputStreamReader(
yc.getInputStream()));
String inputLine;
while ((inputLine = in.readLine()) != null) {
System.out.println(inputLine);
} // end while
in.close();
JSONObject obj = new JSONObject(inputLine);
double eps = obj.getJSONObject("earnings").getDouble("actualEPS");
System.out.println("This is the first instance of EPS: " + eps);
} // end getData method
我在堆栈跟踪中得到一个空指针异常:
at org.json.JSONTokener.<init>(JSONTokener.java:94)
at org.json.JSONObject.<init>(JSONObject.java:357)
at code.URLConnectionReader.getData(URLConnectionReader.java:39)
如何解析"actualEPS“的第一个实例以及仅第一个实例的数据?
编辑
JSONObject obj = new JSONObject(inputLine);
JSONArray earningsArray = obj.getJSONArray("earnings");
JSONObject firstEPS = earningsArray.getJSONObject(0);
double eps = firstEPS.getDouble("actualEPS");
System.out.println("This is the first instance of EPS: " + eps);
发布于 2018-09-30 07:52:43
首先,由于我不知道您的json有多大,并且您只想解析json本身的特定部分,所以我建议您使用JsonReader.class而不是JsonObject.class.。
不同之处:
其次,如果你知道你总是只需要你的json的第一个实例,你可以在解析它之前简单地缩短你的jsonString本身(例如substring
等)。
现在来看你的代码:
新的公共静态无效字符串( String % s)抛出异常{String String(theWebSiteImGettingDataFrom); =
getData //分号!URL targetURL =新建URL(urlBase);URLConnection yc = targetURL.openConnection();BufferedReader in =新建BufferedReader( new InputStreamReader( yc.getInputStream();String inputLine;
while ((inputLine = in.readLine()) != null) { System.out.println(inputLine);} // end while in.close();System.out.println("My json:"+inputLine);//TODO:这里的输出是什么?JSONObject obj =新JSONObject(inputLine);double eps =JSONObject System.out.println(“这是eps的第一个实例:”+ eps);
} //结束getData方法
请评论,因为我不知道你是否从你的服务器上得到了json。
EDIT:您的错误在这一行:
double eps = obj.getJSONObject("earnings").getDouble("actualEPS");
简而言之,应该是这样的:
double eps = obj.getJSONArray("earnings").getJSONObject(0).getDouble("actualEPS");
但是,为什么?您的属性indizes返回一个JSONArray (这意味着您有多个带indizes的行)。因此,与其仅仅请求“收益”作为JSONObject,不如将其用作JSONArray,然后提取第一行(= .getJSONObject(0)
)。在提取第一行之后,您可以实际使用双精度值。
我希望这能起作用:)
第二次编辑:改变这..
while ((inputLine = in.readLine()) != null) {
System.out.println(inputLine);} // end while
至:
while ((inputLine += in.readLine()) != null) {
System.out.println(inputLine);} // end while
while循环不断迭代,直到 .readLine()
返回null。因为null是最后一次迭代,所以您的变量中只有null。
按照建议,您只需将=更改为+=即可解决此问题。
我希望我们现在得到了它。:)
https://stackoverflow.com/questions/52573035
复制相似问题