2016-02-08 30 views
0

我找不出爲什麼我的json解析不起作用。這裏是我正在使用的Api。另外的鏈接完整JSON輸出http://api.openweathermap.org/data/2.5/forecast/daily?zip=85008&amode=json&units=metric&cnt=7&APPID=3c6fee6e3e8b5764212701d9535a36d5無法正確解析Json使用翻新

{ 
"city": { 
     "id": 5308655, 
     "name": "Phoenix", 
     "coord": { 
      "lon": -112.074043, 
      "lat": 33.44838 
     }, 
     "country": "US", 
     "population": 0 
    }, 
    "cod": "200", 
    "message": 0.014, 
    "cnt": 7, 
    "list": [ 
     { 
      "dt": 1454871600, 
      "temp": { 
       "day": 10.46, 
       "min": 10.46, 
       "max": 10.46, 
       "night": 10.46, 
       "eve": 10.46, 
       "morn": 10.46 
      }, 
      "pressure": 977.01, 
      "humidity": 32, 
      "weather": [ 
       { 
        "id": 800, 
        "main": "Clear", 
        "description": "sky is clear", 
        "icon": "01n" 
       } 
      ], 
      "speed": 4.1, 
      "deg": 45, 
      "clouds": 0 
     }, 
     { 
      "dt": 1454958000,  
      "temp": { 
       "day": 16.88, 
       "min": 3.31, 
       "max": 24.29, 
       "night": 11.29, 
       "eve": 23.78, 
       "morn": 4.31 
      }, 
      "pressure": 979.15, 
      "humidity": 30, 
      "weather": [ 
       { 
        "id": 800, 
        "main": "Clear", 
        "description": "sky is clear", 
        "icon": "01d" 
       } 
      ], 
      "speed": 2.41, 
      "deg": 52, 
      "clouds": 0 
     }, 

我想從每天的最小和最大的臨時工。這是我的代碼。拋出的異常是期望的BEGIN_OBJECT,但在第1行第190列有NUMBER。任何幫助表示感謝,謝謝!

@Override 
protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.activity_main); 

    ListView listView =(ListView) findViewById(R.id.main_activity_list); 

    arrayAdapter = new ArrayAdapter<>(
      this, 
      android.R.layout.simple_list_item_1, 
      android.R.id.text1, 
      new ArrayList<List1>()); 

    listView.setAdapter(arrayAdapter); 
} 

@Override 
protected void onResume() { 
    super.onResume(); 

    HttpLoggingInterceptor interceptor = new HttpLoggingInterceptor(); 
    interceptor.setLevel(HttpLoggingInterceptor.Level.BODY); 
    OkHttpClient client = new OkHttpClient.Builder().addInterceptor(interceptor).build(); 

    Retrofit retrofit = new Retrofit.Builder() 
      .baseUrl("http://api.openweathermap.org") 
      .client(client) 
      .addConverterFactory(GsonConverterFactory.create()) 
      .build(); 

    WeatherAPI weatherAPI = retrofit.create(WeatherAPI.class); 

    Call<Weather> call = weatherAPI.loadWeather("85008", "json", "metric", "7", "3c6fee6e3e8b5764212701d9535a36d5"); 
    call.enqueue(this); 
} 

@Override 
public void onResponse(Call<Weather> call, Response<Weather> response) { 
    arrayAdapter.clear(); 
    arrayAdapter.addAll(response.body().list); 
} 

@Override 
public void onFailure(Call<Weather> call, Throwable t) { 
    Log.v(MainActivity.class.getSimpleName(), t.getLocalizedMessage()); 
} 


public interface WeatherAPI { 
    @GET("/data/2.5/forecast/daily") 
    Call<Weather> loadWeather(
      @Query("zip")String zip, 
      @Query("amode")String amode, 
      @Query("units")String units, 
      @Query("cnt")String cnt, 
      @Query("APPID")String APIKey); 
} 



public class Weather{ 
    public List<List1> list; 
} 


public class List1{ 
    double dt; 
    public HashMap<String, Temps> temp; 

    @Override 
    public String toString() { 
     String output = "Min and High "; 

     for(Map.Entry<String,Temps> temps:temp.entrySet()){ 
      output += temps.getKey() + " = " + temps.getValue().min; 
     } 
     return output; 


    } 
} 


public class Temps{ 
    double min; 
    double max; 

} 
+0

可以請您分享一下WeatherAPI嗎? – VVB

+0

我認爲把temp當做散列表示在list1類中是不對的,它應該是一個單獨的對象'Temps temp;',因爲在json中它是每個列表項的1個'temp' – Yazan

+0

就是這樣!謝謝雅贊 –

回答

0

temp不是一個數組,它的類對象。

你應該知道改造的一件事不會支持直接散列圖​​檢索(Pojo方法)。

public HashMap<String, Temps> temp; - 這種方法是錯誤的。

public Temps temp; - 這是正確的。

如果您想將您的回覆存儲在HashMap中,還有其他解決方法,您應該看一下。

0

由於Yazan答案是

public class Weather{ 
    public List<List1> list; 
} 


public class List1{ 
    double dt; 
    public Temps temp; 


    @Override 
    public String toString() { 
     return "List1{" + 
       "temp min = " + temp.min + " temp max " + temp.max + " dt = "+ dt+ 
       '}'; 
    } 
} 


public class Temps{ 
    double min; 
    double max; 
} 

}

凡不是由一個HashMap顯示臨時工而是一個對象。我希望這能幫助我認識的人幫助我。