Java 第一元素JSONArray

Java 第一元素JSONArray,java,android,Java,Android,我尝试用这本书制作应用程序,在我看来我发现了这本书中的错误 我们可以在这本书中看到: 此行读取“天气”表,访问 包含天气状况描述的数组 日期和图示图标 他们给出了这条线: JSONObject weather = day.getJSONArray("weather").getJSONObject(0); 从这个班 private void convertJSONtoArrayList(JSONObject forecast) { weatherList

我尝试用这本书制作应用程序,在我看来我发现了这本书中的错误 我们可以在这本书中看到:

此行读取“天气”表,访问 包含天气状况描述的数组 日期和图示图标

他们给出了这条线:

JSONObject weather =
               day.getJSONArray("weather").getJSONObject(0);
从这个班

private void convertJSONtoArrayList(JSONObject forecast) {
      weatherList.clear(); 

      try {

         JSONArray list = forecast.getJSONArray("list");


         for (int i = 0; i < list.length(); ++i) {
            JSONObject day = list.getJSONObject(i); 

            JSONObject temperatures = day.getJSONObject("temp");


            JSONObject weather =
               day.getJSONArray("weather").getJSONObject(0);


            weatherList.add(new Weather(
               day.getLong("dt"), 
               temperatures.getDouble("min"), 
               temperatures.getDouble("max"), 
               day.getDouble("humidity"), 
               weather.getString("description"), 
               weather.getString("icon"))); 
         }
      }
      catch (JSONException e) {
         e.printStackTrace();
      }
   }
private void convertJSONtarrayList(JSONObject预测){
天气预报表;
试一试{
JSONArray list=forecast.getJSONArray(“list”);
对于(int i=0;i
我认为这是一个错误(它们强调3和4元素,而不是1):

1{
2“城市”:{
3“id”:5128581,
4“名称”:“纽约”,
5“协调”:{
6“lon”:-74.005966,
7“纬度”:40.714272
8 },
9“国家”:“美国”,
10“人口”:0
11 },
12“cod”:“200”,
13“消息”:0.0102,
14“cnt”:2,
15“名单”:[{
16“dt”:1442419200,
17“温度”:{
18“天”:79.9,
19“分”:71.74,
20“最大值”:82.53,
21“夜”:71.85,
22“夏娃”:82.53,
23“上午”:71.74
24 },
25“压力”:1037.39,
26“湿度”:64,
27“天气”:[{
28“id”:800,
29“主要”:“明确”,

30“说明”:“天空晴朗”,书中没有错误。只是JSON解析

JSONObject weather =
               day.getJSONArray("weather").getJSONObject(0);
上面的live将为您提供一个JSONObject

{
28 "id": 800,
29 "main": "Clear",
30 "description": "sky is clear",       <-- they underline this
31 "icon": "old"                        <-- and this
32 }
{
28“id”:800,
29“主要”:“明确”,
30“说明”:“天空晴朗”,这本书是正确的

请注意,数组中的第一项既不是描述也不是图标。相反,它是一个完整的json对象:

{
"id": 800,
"main": "Clear",
"description": "sky is clear",
"icon": "old"
}

一旦他们使用
getJSONObject(0)
获得这个对象,他们就会访问它的描述和图标条目(顺便说一句,这是无序的,因为对象中的json条目是无序的)。

书中的示例是正确的,可以解释如下。 在上面的代码段中,它为每个数组元素读取以下值:

  • 临时对象的最小和最大关键点,其值对应于第一个对象,如下所示:
    • 71.74
    • 82.53
  • 湿度键,其值对应于第一个对象,如下所示:
    • 六十四
  • 天气对象的描述和图标键,其值对应于第一个对象,如下所示:
    • “天晴”
    • “旧的”

如果你认为这本书有错误,那就去问这本书的作者,而不是我们。我们没有写这本书。你也没有问任何问题。你有什么问题吗?那么速度、度和云是数组“天气”的第二个元素?那么速度、度和云是数组“天气”的第二个元素?
{
"id": 800,
"main": "Clear",
"description": "sky is clear",
"icon": "old"
}