zoukankan      html  css  js  c++  java
  • Android数据篇Json解析

    现在的手机客户端一般是通过Json格式来传输数据,因为Json数据是轻量级的,便于传输。

    在通过HTTP拿到服务端返送的Json数据后,就会有一个解析的问题。

    解析的方式有很多种,比如说Google的Gson...

    这里只讲一种方法

        /**解析JSON数据
         * @param strJSON Json数据串
         * @return HashMap键值对
         * @throws JSONException
         */
        public static HashMap<String, Object> parseJSON(String strJSON)
            throws JSONException {
            if(strJSON == null){
                return null;
            }
            HashMap<String, Object> result = new HashMap<String, Object>();
            
            /**蒋Json数据串填充进入一个JSONObject对象,
             * 然后遍历取键值对
             * 根据HashMap对象进行后面的处理*/
            JSONObject jo = new JSONObject(strJSON);
            Iterator iterator = jo.keys();
            String key;
            while (iterator.hasNext()) {
                key = iterator.next().toString();
                result.put(key, jo.get(key));
            }
            return result;
        }

    result就是根据HTTP请求拿到的JSON串转化过来的一个HashMap对象。

    当我们拿到这个HashMap的对象后,我们就可以根据接口文档提取有关字段的数据,

    直接把拿到的数据Set给你已经写好的实体类的相关属性。

  • 相关阅读:
    [bzoj4893]项链分赃
    [Spoj]Counting Divisors (cube)
    [Noi2016]国王饮水记
    [Noi2016]网格
    [Noi2016]优秀的拆分
    [Noi2016]区间
    [Noi2015]寿司晚宴
    Codeforces Round #411 (Div. 2)
    VK-Cup2017 Wild Card Round 2
    [Noi2015]小园丁和老司机
  • 原文地址:https://www.cnblogs.com/gongcb/p/2494562.html
Copyright © 2011-2022 走看看