【问题标题】:Cannot Correctly Parse Json Using retrofit无法使用改造正确解析 Json
【发布时间】:2016-02-08 06:38:05
【问题描述】:

我无法弄清楚为什么我的 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  
        },

我正在尝试获取每天的最低和最高温度。这是我的代码。抛出的异常是 Expected 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;

}

【问题讨论】:

  • 可以分享一下 WeatherAPI 吗?
  • 我认为在 list1 类中将 Temp 表示为哈希是不正确的,它应该是单个对象 Temps temp; 因为在 json 中它是 1 temp 每个列表项
  • 就是这样!谢谢雅赞

标签: android json retrofit


【解决方案1】:

temp 不是一个数组,它的类对象。

你应该知道改造不支持直接哈希映射检索(Pojo 方法)。

public HashMap&lt;String, Temps&gt; temp; -- 这种做法是错误的。

public Temps temp; -- 这是正确的。

如果您想将您的回复存储在HashMap,还有其他一些解决方法,您应该看看。

【讨论】:

    【解决方案2】:

    感谢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 不是由 hashmap 而是由单个对象显示的。我希望这对我认识的人有所帮助。

    【讨论】:

      猜你喜欢
      • 2018-05-17
      • 2018-02-12
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-04-02
      • 2020-07-24
      • 2023-03-23
      相关资源
      最近更新 更多