Android学习-JSON数据格式

Android访问某个网页时网页会返回一个Inputstream流,这个流所包含的内容是一个json对象,比如天气预报网页的接口(http://www.weather.com.cn/data/cityinfo/101270101.html) ,如果把这个json对象当作String类来处理的话,就太麻烦了。还好,对于这样结构的数据,有json对象帮我们处理。具体可以这样。因为SDK中已经提供了JSON类,所以我们可以直接用,不用像java中要自己导入对于的jar包。

具体包在:org.json.* ; 在这里包下面有4个json类和一个json异常,分别是JSONArray   JSONObject  JSONStringer  JSONTokener JSONException

Public void getWeather(){

      String TAG= “Weather” ;

String str = “” ;

String date = “” ;

url = new URL(urlString);

    httpConn = (HttpURLConnection) url.openConnection();

   InputStream in = httpConn.getInputStream();

BufferedReader br = new BufferedReader(

new InputStreamReader(in,"UTF-8"));

while((str=br.readLine()) !=null){

        Log.e(TAG, "天气内容是:"+str);

        date += str ;

}

JSONObject jsonDate = new JSONObject(date);

SONObject jsonWeather = jsonDate.getJSONObject("weatherinfo");

 

Log(TAG, jsonDate.getJSONObject("city"));

Log(TAG, jsonDate.getJSONObject("cityid "));

Log(TAG, jsonDate.getJSONObject("weather "));

Log(TAG, jsonDate.getJSONObject("temp2"));

Log(TAG, jsonDate.getJSONObject("temp1"));

Log(TAG, jsonDate.getJSONObject("ptime "));

}

 

 

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值