无法访问JSON数组中的第一个数据项

时间:2018-10-04 01:49:52

标签: arrays json

我无法从JSON数组中提取一条数据。我相信我已经尝试过这里类似帖子的所有答案,但我缺少一些东西。我已经导入了org.JSON库。这是我的JSON文本:

{
  "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.29        940119760479045,
    "symbolId":11
  },
  {
    "actualEPS":2.73,
    "consensusEPS":2.69,
    ...
    ...
  }
]

我正在尝试读取“ actualEPS​​”的第一个实例,数组中共有四个。我的代码当前如下所示:

    String jsonIn = sb.toString();
    JSONArray earningsArray = new JSONArray(jsonIn);
    double eps = 0;

    for(int i = 0; i < earningsArray.length(); i++) {
        JSONObject jObject = earningsArray.getJSONObject(i);
         eps = jObject.getDouble("actualEPS");
    } // end for loop

    System.out.println("This is the first instance of EPS: " + eps);

第一行中的StringBuilder(sb)是我的JSON,并在此块之前的控制台中正确打印出。

堆栈跟踪在此行中显示错误:

    JSONArray earningsArray = new JSONArray(jsonIn);

我遇到的错误是
“ JSONArray文本必须以'['开头1 [字符2第1行]“

我之前从未使用过JSON,并且不确定我的错误到底是什么。我尝试缩短进入数组的字符串,使其仅从数组左括号开始,但这也不起作用。我觉得我缺少一些简单的东西。我在哪里弄错了?

3 个答案:

答案 0 :(得分:0)

@ Driver8,JSON earninsArray是单个JSON对象,而不是JSON对象数组。在这里,您的基本想法很适合该行。实例化 JSON对象,而不是实例化JSON数组。

应该是这样的:

String jsonIn = sb.toString();
JSONObject earningsObject = new JSONObject(jsonIn);
JSONArray earningsArray = earningsObject.getJSONArray("earnings");
double eps = new double(earningsArray.length());

for (int i = 0; i < earningsArray.length(); i++) {
    JSONObject j1 = earningsArray.getJSONObject(i);
    eps[i] = j1.getDouble("actualEPS");
}

actualEPS的所有值将按照其顺序存储在声明为存储EPS值的数组中。

希望这会有所帮助。

答案 1 :(得分:0)

执行此操作:

JSONObject json = new JSONObject(response);
JSONArray data = json.optJSONArray("earnings");

for (int i = 0; i < data.length(); i++) {

   JSONObject jsonObject1 = data.getJSONObject(i);

   Double actualEPS = jsonObject1.getDouble("actualEPS");
   Double consensusEPS = jsonObject1.getDouble("consensusEPS");
   Double estimatedEPS= jsonObject1.getDouble("estimatedEPS");
   String announceTime = jsonObject1.getString("announceTime");
   int numberOfEstimates = jsonObject1.getInt("numberOfEstimates");
   Double EPSSurpriseDollar= jsonObject1.getDouble("EPSSurpriseDollar");
   String EPSReportDate= jsonObject1.getString("EPSReportDate");
   String fiscalPeriod= jsonObject1.getString("fiscalPeriod");
   String fiscalEndDate= jsonObject1.getString("fiscalEndDate");
   Double yearAgo= jsonObject1.getDouble("yearAgo");
   Double yearAgoChangePercent= jsonObject1.getDouble("yearAgoChangePercent");
   Double estimatedChangePercent = jsonObject1.getDouble("estimatedChangePercent");
   int symbolId = jsonObject1.getInt("symbolId");
}

答案 2 :(得分:0)

您需要了解JSON中有两种类型的数据结构。

  1. 第一个是对象,该对象始终以“ {”开头,并以“}”结尾

  2. second是始终以'['开始,以']'结束的Array

JsonArray没什么,但JsonObject数组意味着json数组将始终像

"jsonarray":[
    {
    //json object
    },
    {
    // json object
    }
]

现在希望您了解json的工作原理

现在进入您的json

{  // jsonObjectParent Starts
  "symbol":"AAPL",
  "earnings":[  // jsonArray Starts
     { //jsonObject1 Starts
       "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.29        940119760479045,
       "symbolId":11
     },  // jsonOject1 Ends
     { //jsonObject2
       "actualEPS":2.73,
       "consensusEPS":2.69,
       ...
       ...
     }
   ]   //jsonArray Ends
 }    //jsonObjectParent Ends

因此在这里,如果您想解析此json,则必须首先在jsonObject中对其进行解析,如您在上面看到的

JsonObject jsonObjectParent = new JsonObject(jsonIn);
// here jsonobject contains json array so etract it like this
JsonArray jsonArray = jsonObjectParent.getJsonArray("earnings");
//now you can access the values here
JsonObject jsonObject1 = jsonArray.getJsonObject(0); // here 0 means first jsonObject in array if you want all you can make a loop here
string actualEps = jsonObject1.getDouble("actualEPS");

希望现在您了解JSON的工作原理

请让我知道此解决方案是否有效

相关问题