try {
JSONObject jsonObject = new JSONObject(json)
JSONObject weatherinfo = jsonObject.getJSONObject("weatherinfo")
System.out.println(weatherinfo.getString("city"))
System.out.println(weatherinfo.getString("cityid"))
System.out.println(weatherinfo.getString("temp"))
System.out.println(weatherinfo.getString("WD"))
} catch (JSONException e) {
e.printStackTrace()
}
其中第一行代码 JSONObject jsonObject = new JSONObject(json)//json 即为你的字符串
比如现在天气信息是多个城市的,并非只有北京市。
{"weatherinfo":[{"city":"北京","cityid":"101010100","temp":"4","WD":"东风","WS":"2级","SD":"75%","WSE":"2","time":"10:45","isRadar":"1","Radar":"JC_RADAR_AZ9010_JB","njd":"暂无实况","qy":"1011"},{"city":"天津","cityid":"101010100","temp":"4","WD":"东风","WS":"2级","SD":"75%","WSE":"2","time":"10:45","isRadar":"1","Radar":"JC_RADAR_AZ9010_JB","njd":"暂无实况","qy":"1011"}]}
以下代码适用。
try {
JSONObject jsonObject=new JSONObject(json)
JSONArray jsonArray=jsonObject.getJSONArray("weatherinfo")
for (int i=0i<jsonArray.length()i++){
JSONObject object = jsonArray.getJSONObject(i)
System.out.println(object.getString("city"))
System.out.println(object.getString("cityid"))
System.out.println(object.getString("temp"))
System.out.println(object.getString("WD"))
}
} catch (JSONException e) {
e.printStackTrace()
}
JSONArray 是json数据格式,它下边包含了jsonObject格式,所以你应该先取jsonObject,如:for(int z = 0z <lengz++){
System.out.println("zzzz"+z)
JSONObject json = jsona.getJSONObject(z)
String name = json.get("name").toString
}
你的jsonarray格式要是正确的话就应该可以拿到name值。
JSONObject的dataJson =新的JSONObject(“你的JSON数据”)JSONObject的响应= dataJson.getJSONObject(“响应”)
JSONArray数据= response.getJSONArray(“数据”) JSONObject的信息= data.getJSONObject(0)
字符串省= info.getString(“省”)
弦乐城市= info.getString(“城市”)
字符串区= info.getString(“区”)
字符串地址= info.getString(“地址”)
System.out.println(省+市+区+地址)