我无法弄清楚为什么我的json Parsing无效。这是我正在使用的Api。也是完整json输出http://api.openweathermap.org/data/2.5/forecast/daily?zip=85008&amode=json&units=metric&cnt=7&APPID=3c6fee6e3e8b5764212701d9535a36d5
的链接{
"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 :(得分: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;
}
}
其中Temps不是由散列图显示,而是由单个对象显示。我希望这可以帮助我认识的人帮助我。
答案 1 :(得分:0)
temp
不是一个数组,它的类对象。
你应该知道改造的一件事是不支持直接哈希映射检索(Pojo方法)。
public HashMap<String, Temps> temp;
- 这种做法是错误的。
public Temps temp;
- 这是正确的。
如果您想将回复存储在HashMap
中,还有其他一些解决方法,您应该看一下。