android中使用JAVA解析json数据
{"weatherinfo":{"city":"北京","cityid":"101010100","temp":"4","WD":"东风","WS":"2级","SD":...
{"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"}}
我想得到的结果是北京,4,东方,2级,75%,2,10:45,1,JC_RADAR_AZ9010_JB 展开
我想得到的结果是北京,4,东方,2级,75%,2,10:45,1,JC_RADAR_AZ9010_JB 展开
2个回答
展开全部
这个格式使用如下代码解析
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=0;i<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();
}
展开全部
import org.json.JSONException;
import org.json.JSONObject;
public class test {
/**
* @param args
*/
public static void main(String[] args) {
String s = "{\"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\"}}";
try {
JSONObject jsonObject = new JSONObject(s);
JSONObject json = (JSONObject) jsonObject.get("weatherinfo");
System.out.println(json.getString("city"));
System.out.println(json.getInt("temp"));
System.out.println(json.getString("WD"));
System.out.println(json.getString("WS"));
System.out.println(json.getString("SD"));
System.out.println(json.getInt("WSE"));
System.out.println(json.getString("time"));
System.out.println(json.getString("isRadar"));
System.out.println(json.getString("Radar"));
} catch (JSONException e) {
e.printStackTrace();
}
}
}
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询