在做json转换的时候要用到的是fastjson这个依赖,maven工程可以直接导入依赖,不是maven工程网上有相应的jar包
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>fastjson</artifactId>
<version>1.2.54</version>
</dependency>
string转json
jsonObject = (JSONObject) JSONObject.parseObject(respon);
java对象转json
JSON json= (JSON) JSON.toJSON(response);
jsonObject中取值
jsonObject.getJSONObject("HeWeather6");//获取出来的是jsonobject对象
// 链式编程调用
jsonObject.getJSONObject("HeWeather6").getJSONObject("daily_forecast")
//乱七八糟的测试小程序
JSONObject jsonObject = HttpUtil.get("https://free-api.heweather.net/s6/weather?key=752369b7d6834c8e86d05ad4396ae71e&location=city");
JSONArray jsonArray = jsonObject.getJSONArray("HeWeather6");
Object o = jsonArray.get(0);
JSONObject jsonObject1 = (JSONObject)o;
// List list = jsonArray.toJavaList(HeFengEntity.class);
System.out.println( jsonObject1.getJSONArray("daily_forecast"));