android天气json解析怎么写

1.android中使用JAVA解析json数据这个格式使用如下代码解析
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
2.Android 解析这样的json数据怎么解析简单的给你做了下解析,具体要解析到什么程度,你自己看吧 。大概解析方法就是这样的:
如下:
privatevoid testJson(){
String JsonData = "http://www.xuexi88.com/zhishi/[{/"id\":[\"386\",\"381\",\"379\",\"377\"],\"num\":[\"386\",\"381\",\"379\",\"377\"]},{\"id\":[\"3860\",\"3810\",\"3790\",\"3770\"],\"num\":[\"3860\",\"3810\",\"3790\",\"3770\"]}]";
JSONObject obj = null;
JSONArray jsonArary;
try {
jsonArary = new JSONArray(JsonData);
for(int i=0;i<jsonArary.length();i++){
obj = jsonArary.getJSONObject(i);
//取ID
JSONArray strID = obj.getJSONArray("id");
for(int j=0;j<strID.length();j++){
Log.v("ID:", strID.get(j).toString());
}
//取num
JSONArray strNum = obj.getJSONArray("num");
for(int k=0;k<strNum.length();k++){
Log.v("NUM:", strNum.get(k).toString());
}
}
} catch (JSONException e) {
e.printStackTrace();
}
}
3.如何在Android中解析JSON 数据在 Android 中解析JSON很方便,但是 JSON 的格式或者层级各有不同,搞的有人好像就是不能入门,下面简单写下~
标准的 json 格式一般是这样的
{ "background" : "true", "expires" : "3600000", "items" : [{ "author" : "vincent4j","created_at" : "2013-05-05T13:14:38+08:00","id" : 512},{ "author" : "vincent4j","created_at" : "2013-05-05T13:14:38+08:00","id" : 512}]}
那么其最外层的是个 {} ,也就是其是个 JSON 对象,直接转成JSONObject,再看 items 这个下面是个 [] ,也就是个数组,那么直接getJSONArray就可以得到了,代码如下:
JSONObject josnObject = new JSONObject(str);JSONArray array = josnObject.getJSONArray("items");int length = array.length();for (int i = 0; i < length; i++) { JSONObject object = array.getJSONObject(i); //Do your code Log.v("json object", object.getString("id"));}上面这个是最常见的格式了,但是有的时候你会遇到这样的 json 格式
{ "response" : { "items" : [ { "detail_url" : "/uc_server/avatar.php?uid=739935&size=small", "id" : "3348", "name" : "过期的白砂糖","short_content" : "GitHub上最火的40个Android开源项目(一)GitHub上最 。","time" : "1367809230", "title" : "GitHub上最火的40个Android开源项目(一)" },{ "detail_url" : "/uc_server/avatar.php?uid=2&size=small", "id" : "18229", "name" : "iceskysl", "short_content" : "OS X Mavericks新功能介绍: /o 。", "time" : "1382471844", "title" : "修改DNS大幅提高OS X Mavericks(5.29G)升级下载速度(4.1M/s)" }]}}
看到区别了没,多了一层,那我们就可以这样来调