青峰码农 2015-09-15 11:26 采纳率: 60%
浏览 3392
已采纳

Gson获得json数据数组内嵌问题

{
"resultcode": "200",
"reason": "successed!",
"result": {
"sk": {
"temp": "24",
"wind_direction": "西风",
"wind_strength": "2级",
"humidity": "64%",
"time": "17:38"
},
"today": {
"temperature": "19℃~25℃",
"weather": "阴转小雨-中雨",
"weather_id": {
"fa": "02",
"fb": "21"
},
"wind": "旋转风微风",
"week": "星期二",
"city": "绵阳",
"date_y": "2015年09月15日",
"dressing_index": "舒适",
"dressing_advice": "建议着长袖T恤、衬衫加单裤等服装。年老体弱者宜着针织长袖衬衫、马甲和长裤。",
"uv_index": "最弱",
"comfort_index": "",
"wash_index": "不宜",
"travel_index": "较适宜",
"exercise_index": "较适宜",
"drying_index": ""
},
"future": [
{
"temperature": "19℃~25℃",
"weather": "阴转小雨-中雨",
"weather_id": {
"fa": "02",
"fb": "21"
},
"wind": "旋转风微风",
"week": "星期二",
"date": "20150915"
},
{
"temperature": "18℃~21℃",
"weather": "中雨",
"weather_id": {
"fa": "08",
"fb": "08"
},
"wind": "东北风微风",
"week": "星期三",
"date": "20150916"
},
{
"temperature": "17℃~21℃",
"weather": "中雨",
"weather_id": {
"fa": "08",
"fb": "08"
},
"wind": "东北风微风",
"week": "星期四",
"date": "20150917"
},
{
"temperature": "19℃~24℃",
"weather": "小雨转阴",
"weather_id": {
"fa": "07",
"fb": "02"
},
"wind": "旋转风微风",
"week": "星期五",
"date": "20150918"
},
{
"temperature": "19℃~26℃",
"weather": "阴",
"weather_id": {
"fa": "02",
"fb": "02"
},
"wind": "旋转风微风",
"week": "星期六",
"date": "20150919"
},
{
"temperature": "19℃~27℃",
"weather": "阴",
"weather_id": {
"fa": "02",
"fb": "02"
},
"wind": "旋转风微风",
"week": "星期日",
"date": "20150920"
},
{
"temperature": "19℃~24℃",
"weather": "阴",
"weather_id": {
"fa": "02",
"fb": "02"
},
"wind": "旋转风微风",
"week": "星期一",
"date": "20150921"
}
]
},
"error_code": 0
}
通过
Gson gson = new Gson();
Book book = gson.fromJson(result, Book.class);
Log.i("info", book.getReason());
我能够得到 reason 的success的值,但是我想要得到result里面的today里面的week的值,请问我要怎么做?数组里面嵌套着数组,我迷茫了。
book.java如下:
public class Book {

private String reason;

public String getReason() {
    return reason;
}

public void setReason(String reason) {
    this.reason = reason;
}}  
  • 写回答

5条回答 默认 最新

  • qq_29076343 2015-09-16 05:50
    关注

    public static List getWeather(String json) {
    List weatherModelList = new ArrayList();
    //System.out.println(json + "json");

        JSONTokener jsonParser = new JSONTokener(json);
        WeatherModel weatherModel = null;
        try {
            JSONObject object = (JSONObject) jsonParser.nextValue();
            System.out.println(object.getString("reason") + "---reason");
            if (object.getString("reason").equals("成功")) {
    
                JSONObject result = object.getJSONObject("result");
                JSONObject sk = result.getJSONObject("sk");
                weatherModel = new WeatherModel();
                weatherModel.setTemp(sk.getString("temp"));
                weatherModel.setWindDirection(sk.getString("wind_direction"));
                weatherModel.setWindStrength(sk.getString("wind_strength"));
                weatherModel.setHumidity(sk.getString("humidity"));
                weatherModel.setTime(sk.getString("time"));
                weatherModelList.add(weatherModel);
                weatherModel = null;
    
                JSONObject today = result.getJSONObject("today");
                weatherModel = new WeatherModel();
                weatherModel.setCity(today.getString("city"));
                weatherModel.setDatey(today.getString("date_y"));
                weatherModel.setWeek(today.getString("week"));
                weatherModel.setTemperature(today.getString("temperature"));
                weatherModel.setWeather(today.getString("weather"));
                weatherModel.setFa(today.getString("fa"));
                weatherModel.setFb(today.getString("fb"));
                weatherModel.setWind(today.getString("wind"));
                weatherModel.setDressing_index(today.getString("dressing_index"));
                weatherModel.setDressing_advice(today.getString("dressing_advice"));
                weatherModel.setUv_index(today.getString("uv_index"));
                weatherModel.setComfort_index(today.getString("comfort_index"));
                weatherModel.setWash_index(today.getString("wash_index"));
                weatherModel.setTravel_index(today.getString("travel_index"));
                weatherModel.setExercise_index(today.getString("exercise_index"));
                weatherModel.setDrying_index(today.getString("drying_index"));
                weatherModelList.add(weatherModel);
                weatherModel = null;
                JSONArray arr = result.getJSONArray("future");
    
    
                for (int i = 0; i < arr.length(); i++) {
                    JSONObject cobject = (JSONObject) arr.get(i);
                    weatherModel = new WeatherModel();
                    weatherModel.setTemperature(cobject.getString("temperature"));
                    weatherModel.setWeather(cobject.getString("weather"));
                    weatherModel.setFa(cobject.getString("fa"));
                    weatherModel.setFb(cobject.getString("fb"));
                    weatherModel.setWind(cobject.getString("wind"));
                    weatherModel.setWeek(cobject.getString("week"));
                    weatherModel.setDate(cobject.getString("date"));
                    weatherModelList.add(weatherModel);
                }
            }
    
    
        } catch (JSONException e) {
            e.printStackTrace();
        }
                我是这样写的
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(4条)

报告相同问题?

悬赏问题

  • ¥50 易语言把MYSQL数据库中的数据添加至组合框
  • ¥20 求数据集和代码#有偿答复
  • ¥15 关于下拉菜单选项关联的问题
  • ¥20 java-OJ-健康体检
  • ¥15 rs485的上拉下拉,不会对a-b<-200mv有影响吗,就是接受时,对判断逻辑0有影响吗
  • ¥15 使用phpstudy在云服务器上搭建个人网站
  • ¥15 应该如何判断含间隙的曲柄摇杆机构,轴与轴承是否发生了碰撞?
  • ¥15 vue3+express部署到nginx
  • ¥20 搭建pt1000三线制高精度测温电路
  • ¥15 使用Jdk8自带的算法,和Jdk11自带的加密结果会一样吗,不一样的话有什么解决方案,Jdk不能升级的情况