zoukankan      html  css  js  c++  java
  • Android学习之Json数据的获取与解析

    Android获取json数据的原理,我的理解是:首先通过http协议获取json数据的字符串格式数据,然后再把字符串格式转变成Json对象的数据

    首先我先将某个网址path的数据获取到:

     1 /**
     2  * HttpUtils.java [V 1.0.0]
     3  * classes :com.oysd.json.HttpUtils
     4  * ouyangshengduo create at 2015-6-24
     5  */
     6 package com.oysd.json;
     7 
     8 import java.io.ByteArrayOutputStream;
     9 import java.io.IOException;
    10 import java.io.InputStream;
    11 import java.net.HttpURLConnection;
    12 import java.net.MalformedURLException;
    13 import java.net.URL;
    14 
    15 /**
    16  * com.oysd.json.HttpUtils
    17  * @author ouyangshengduo
    18  * create at 2015-6-24
    19  */
    20 public class HttpUtils {
    21 
    22     public HttpUtils() { 
    23     } 
    24      
    25     public static String getJsonContent(String path){ 
    26         try { 
    27             URL url = new URL(path); 
    28             HttpURLConnection connection = (HttpURLConnection)url.openConnection(); 
    29             connection.setConnectTimeout(3000); 
    30             connection.setRequestMethod("GET"); 
    31             connection.setDoInput(true); 
    32             int code = connection.getResponseCode(); 
    33             if(code == 200){ 
    34                 //获取数据输入流
    35                 return changeInputStream(connection.getInputStream()); 
    36             } 
    37         } catch (MalformedURLException e) { 
    38             e.printStackTrace(); 
    39         } catch (IOException e) { 
    40             e.printStackTrace(); 
    41         } 
    42         return null; 
    43     } 
    44  
    45     /** 
    46      * 将一个输入流转换成指定编码的字符串
    47      * @param inputStream 
    48      * @return 
    49      */ 
    50     private static String changeInputStream(InputStream inputStream) { 
    51         String jsonString = ""; 
    52         ByteArrayOutputStream outputStream = new ByteArrayOutputStream(); 
    53         int len = 0; 
    54         //把输入流转换成字符串
    55         byte[] data = new byte[1024]; 
    56         try { 
    57             while((len=inputStream.read(data))!=-1){ 
    58                 outputStream.write(data,0,len); 
    59             } 
    60             jsonString = new String(outputStream.toByteArray()); 
    61         } catch (IOException e) { 
    62             e.printStackTrace(); 
    63         } 
    64         return jsonString; 
    65     } 
    66 }
    View Code

    获取到页面的字符串数据之后,就是解析这些数据,这些数据有可能是:一个对象,对象数组,String数组,对象的Map集合。

    一下是针对这些基本的数据格式的解析方法:

      1 /**
      2  * JsonTools.java [V 1.0.0]
      3  * classes :com.oysd.json.JsonTools
      4  * ouyangshengduo create at 2015-6-24
      5  */
      6 package com.oysd.json;
      7 
      8 import java.util.ArrayList;
      9 import java.util.HashMap;
     10 import java.util.Iterator;
     11 import java.util.List;
     12 import java.util.Map;
     13 
     14 import org.json.JSONArray;
     15 import org.json.JSONException;
     16 import org.json.JSONObject;
     17 
     18 /**
     19  * com.oysd.json.JsonTools
     20  * @author ouyangshengduo
     21  * create at 2015-6-24
     22  */
     23 public class JsonTools {
     24 
     25     public JsonTools() { 
     26         
     27     } 
     28      
     29     /** 
     30      * 获取对象数据 
     31      * @param key 
     32      * @param jsonString 
     33      * @return 
     34      */ 
     35     public static Person getPerson(String key,String jsonString){ 
     36         Person person = new Person(); 
     37         try { 
     38             JSONObject jsonObject = new JSONObject(jsonString); 
     39             JSONObject personObject = jsonObject.getJSONObject(key); 
     40             person.setId(personObject.getInt("id")); 
     41             person.setName(personObject.getString("name")); 
     42             person.setAddress(personObject.getString("address")); 
     43         } catch (JSONException e) { 
     44             e.printStackTrace(); 
     45         } 
     46         return person; 
     47     } 
     48      
     49     /** 
     50      * 获取对象数组数据 
     51      * @param key 
     52      * @param jsonString 
     53      * @return 
     54      */ 
     55     public static List<Person> getPersons(String key,String jsonString){ 
     56         List<Person> list = new ArrayList<Person>(); 
     57         try { 
     58             JSONObject jsonObject = new JSONObject(jsonString); 
     59             //返回json数组数据
     60             JSONArray jsonArray = jsonObject.getJSONArray(key); 
     61             for(int i=0;i<jsonArray.length();i++){ 
     62                 JSONObject jsonObject2 = jsonArray.getJSONObject(i); 
     63                 Person person = new Person(); 
     64                 person.setId(jsonObject2.getInt("id")); 
     65                 person.setName(jsonObject2.getString("name")); 
     66                 person.setAddress(jsonObject2.getString("address")); 
     67                 list.add(person); 
     68             } 
     69         } catch (JSONException e) { 
     70             e.printStackTrace(); 
     71         } 
     72          
     73         return list; 
     74     } 
     75     /** 
     76      * 获取String数组数据
     77      * @param key 
     78      * @param jsonString 
     79      * @return 
     80      */ 
     81     public static List<String> getList(String key,String jsonString){ 
     82         List<String> list = new ArrayList<String>(); 
     83         try { 
     84             JSONObject jsonObject = new JSONObject(jsonString); 
     85             JSONArray jsonArray = jsonObject.getJSONArray(key); 
     86             for(int i=0;i<jsonArray.length();i++){ 
     87                 String msg = jsonArray.getString(i); 
     88                 list.add(msg); 
     89             } 
     90         } catch (JSONException e) { 
     91             e.printStackTrace(); 
     92         } 
     93         return list; 
     94     } 
     95     /** 
     96      * 获取对象的Map集合数据 
     97      * @param key 
     98      * @param jsonString 
     99      * @return 
    100      */ 
    101     public static List<Map<String,Object>> getListMap(String key,String jsonString){ 
    102         List<Map<String,Object>> list = new ArrayList<Map<String,Object>>(); 
    103         try { 
    104             JSONObject jsonObject = new JSONObject(jsonString); 
    105             JSONArray jsonArray = jsonObject.getJSONArray(key); 
    106             for(int i=0;i<jsonArray.length();i++){ 
    107                 JSONObject jsonObject2 = jsonArray.getJSONObject(i); 
    108                 Map<String,Object> map = new HashMap<String, Object>(); 
    109                 Iterator<String> iterator = jsonObject2.keys(); 
    110                 while(iterator.hasNext()){ 
    111                     String json_key = iterator.next(); 
    112                     Object json_value = jsonObject2.get(json_key); 
    113                     if(json_value==null){ 
    114                         json_value = ""; 
    115                     } 
    116                     map.put(json_key, json_value); 
    117                 } 
    118                 list.add(map); 
    119             } 
    120         } catch (JSONException e) { 
    121             // TODO Auto-generated catch block 
    122             e.printStackTrace(); 
    123         } 
    124          
    125         return list; 
    126     } 
    127     
    128 }
    View Code


    返回的信息就是我们想要得到的数据了,了解一下这个过程就好了。

  • 相关阅读:
    【Django】Django REST Framework简单教程
    【SpringBoot | Druid】SpringBoot整合Druid
    【SpringBoot | Swagger】SpringBoot整合Swagger
    【Vue | ElementUI】Vue离开当前页面时弹出确认框实现
    【SpringBoot | Redis】SpringBoot整合Redis
    【评测机】评测时报错cc1plus: fatal error: /xx/xx/main.cpp: Permission denied compilation terminated.的解决方法
    Hello world!
    【续】将HUSTOJ备份数据并迁移至Ubuntu16.04
    将HUSTOJ备份数据并迁移至Ubuntu16.04
    【BZOJ2118】墨墨的等式【循环节做法】
  • 原文地址:https://www.cnblogs.com/ouyangduoduo/p/4597230.html
Copyright © 2011-2022 走看看