Android之解析Json数据 .

来源:互联网 发布:linux usr目录 编辑:程序博客网 时间:2024/05/17 08:01
JSON(JavaScript Object Notation) 是一种轻量级的数据交换格式。它基于JavaScript(Standard ECMA-262 3rd Edition - December 1999)的一个子集。

在Android中被广泛运用于客户端和网络(或者说服务器)通信。

JSON 表示名称 / 值对的方式

  按照最简单的形式,可以用下面这样的 JSON 表示"名称 / 值对":

            { "name": "Brett", "lage":22,"sex": "女" } ,这表示了一个JsonObject。

            [{name:"张三:",age:21,sex:"女"},{name:"李斯",age:21,sex:"女"},{name:"王五",age:21,sex:"女"}],使用中括弧表示JsonArray,是json对象数组。

       一、解析第一种单个json对象的json数据。数据从网络上获取。演示实例为 查询手机号码归属地。

      

[java] view plaincopyprint?
  1. URL url;  
  2.         StringBuffer sb = new StringBuffer();  
  3.         String line = null;  
  4.         try {  
  5.             url = new URL(  
  6.             "http://api.showji.com/Locating/default.aspx?m=13763089126&output=json&callback=querycallback");  
  7.             HttpURLConnection conn = (HttpURLConnection) url.openConnection();  
  8.             InputStream is = conn.getInputStream();  
  9.             BufferedReader buffer = new BufferedReader(  
  10.                     new InputStreamReader(is));  
  11.             while ((line = buffer.readLine()) != null) {  
  12.                 sb.append(line);  
  13.             }  
  14.   
  15.         } catch (MalformedURLException e) {  
  16.             e.printStackTrace();  
  17.         } catch (IOException e) {  
  18.             e.printStackTrace();  
  19.         }  

此处获取的数据为:

querycallback({"Mobile":"13763******","QueryResult":"True","Province":"广东","City":"湛江","AreaCode":"0759","PostCode":"524000","Corp":"中国移动","Card":"GSM"});

需要截取这个json对象出来。

String js = sb.substring(sb.indexOf("{"), sb.indexOf("}") + 1);

 

下面函数解析json对象,返回一个Callerloc对象

Callerloc是一个实体类

[html] view plaincopyprint?
  1. private Callerloc parse(String json) {  
  2.         Callerloc my = null;  
  3.   
  4.         if (json == null || json.length() < 1)  
  5.             return null;  
  6.         try {  
  7.             my = new Callerloc();  
  8.             JSONObject jsonobj = new JSONObject(json);  
[html] view plaincopyprint?
  1. my.setMobile(jsonobj.getString("Mobile"));  
  2. my.setQueryResult(jsonobj.getString("QueryResult"));  
  3. my.setProvince(jsonobj.getString("Province"));  
  4. my.setCity(jsonobj.getString("City"));  
  5. my.setAreaCode(jsonobj.getString("AreaCode"));  
  6. my.setPostCode(jsonobj.getString("PostCode"));  
  7. my.setCard(jsonobj.getString("Card"));  
  8. my.setCorp(jsonobj.getString("Corp"));  
[html] view plaincopyprint?
  1.     } catch (JSONException e) {  
  2.         e.printStackTrace();  
  3.     }  
  4.     return my;  
  5. }  

       二、解析json数组

            json数据为:[{name:"张三:",age:21,sex:"女"},{name:"李斯",age:21,sex:"女"},{name:"王五",age:21,sex:"女"}]

           返回list

[java] view plaincopyprint?
  1. private ArrayList<myjson> parsem(String json) {  
  2.         myjson my = null;  
  3.   
  4.         if (json == null || json.length() < 1)  
  5.             return null;  
  6.         try {  
  7.             JSONArray jsonary = new JSONArray(json);  
  8.             ArrayList<myjson> objlist = new ArrayList<myjson>();  
  9.             for (int i = 0; i < jsonary.length(); i++) {  
  10.                 my = new myjson();  
  11.                 JSONObject jsonobj = jsonary.getJSONObject(i);  
  12.                 my.set_name(jsonobj.getString("name"));  
  13.                 my.set_age(jsonobj.getInt("age"));  
  14.                 my.set_sex(jsonobj.getString("sex"));  
  15.                 objlist.add(my);  
  16.             }  
  17.             return objlist;  
  18.         } catch (JSONException e) {  
  19.             e.printStackTrace();  
  20.         }  
  21.         return null;  
  22.     }  
原创粉丝点击