zoukankan      html  css  js  c++  java
  • Json解析之FastJson

    版权声明:转载请注明出处 https://blog.csdn.net/heqiangflytosky/article/details/37659943

    1、FastJson介绍

    FastJson是阿里巴巴提供的一个Json处理工具包,具有极快的性能,超越任其它的Java Json parser。使用起来非常方便。

    2、例程

    首先下载fastjson-1.1.27-android.jar包并导入project中:

    DataBean.java:

    package com.heqiang.testactivity;
    
    public class DataBean{
        private String name;
        private String url;
        private String summary;
        private long id;
        public DataBean() {
        }
        
        public DataBean(long id, String name, String url, String summary) {
        	this.id = id;
        	this.name = name;
        	this.url = url;
        	this.summary = summary;
        }
        
        public String getName() {
            return name;
        }
        public void setName(String name) {
            this.name = name;
        }
        public String getUrl() {
            return url;
        }
        public void setUrl(String url) {
            this.url = url;
        }
        public String getSummary() {
            return summary;
        }
        public void setSummary(String summary) {
    		this.summary = summary;
        }
    
    	public long getId() {
    		return id;
    	}
    
    	public void setId(long id) {
    		this.id = id;
    	}
    }

    ResponseBean.java:

    package com.heqiang.testactivity;
    
    public class ResponseBean {
    	private int code;
    	private String message;
    	private Value value;
    
    	public int getCode() {
    		return code;
    	}
    
    	public void setCode(int code) {
    		this.code = code;
    	}
    
    	public String getMessage() {
    		return message;
    	}
    
    	public void setMessage(String message) {
    		this.message = message;
    	}
    
    	public Value getValue() {
    		return value;
    	}
    
    	public void setValue(Value value) {
    		this.value = value;
    	}
    
    	public static class Value {
    		private String data;
    		private boolean flag;
    		
    		public Value() {
    		}
    		
    		public Value(String data, boolean flag) {
    			this.data = data;
    			this.setFlag(flag);
    		}
    
            public String getData() {
    			return data;
    		}
    
    		public void setData(String data) {
    			this.data = data;
    		}
    
    		public boolean isFlag() {
    			return flag;
    		}
    
    		public void setFlag(boolean flag) {
    			this.flag = flag;
    		}
    	}
    
    }


    解析函数:

    	private void testFastJson(){
    		String data = "{"code":200,"message":"","value":{"flag":true,"data":[{"url":"http://www.baidu.com","id":1,"name":"百度","summary":"搜索引擎"},{"url":"http://www.sina.com","id":2,"name":"新浪","summary":"门户站点"},{"url":"http://www.qq.com","id":3,"name":"腾讯","summary":"社交网络"},{"url":"http://www.taobao.com","id":4,"name":"淘宝","summary":"购物站点"}]}}";
    
    		ResponseBean reponse = JSON.parseObject(data, ResponseBean.class);
    		if(reponse != null && reponse.getCode() == 200 && reponse.getValue() != null 
    				&& reponse.getValue().getData() != null){
    			List<DataBean> list = JSON.parseArray(reponse.getValue().getData(), DataBean.class);
    			for (DataBean dataBean : list) {
    				Log.d("Test", "name = "+dataBean.getName()+", url = "+dataBean.getUrl()+", id = "
    						+dataBean.getId()+", summary = "+dataBean.getSummary());
    			}
    		}
    	}

    程序执行后打印例如以下:

    name = 百度, url = http://www.baidu.com, id = 1, summary = 搜索引擎
    name = 新浪, url = http://www.sina.com, id = 2, summary = 门户站点
    name = 腾讯, url = http://www.qq.com, id = 3, summary = 社交网络
    name = 淘宝, url = http://www.taobao.com, id = 4, summary = 购物站点

    3、Bean的命名规则

    Bean中相应的set必须和Json中的字段相应。比方Json中有id字段。get方法名应该为setId。必须是set+字段名,字段名忽略大写和小写,假设字段是相似的is_default,那么set方法名应为setIs_default。下划线不能省略

    4、注意事项

    有一次在项目中用的时候遇到一个小插曲,无论怎么样解析出来的ResponseBean都没有数据。后来才发现是默认做了代码混淆的缘故。在proguard.flags设置对该类不做代码混淆就能够了。

    5、Java对象转Json

    FastJson能够非常方便的把Java的一些实例转化为Json字符串:

    		HashMap<String, String> map = new HashMap<String, String>();
    		map.put("key1", "test1");
    		map.put("key2", "test2");
    		String json = JSON.toJSONString(map);



查看全文
  • 相关阅读:
    互联网流媒体直播点播平台报ioutil.WriteFile错误导致文件只读如何处理?
    互联网直播点播平台go语言搭建重定向和反向代理的区别及使用
    互联网视频直播点播平台EasyDSS如何集成流媒体平台调取登录及上传接口?
    互联网直播点播平台如何联合RTMP推流摄像头构建智慧消防方案?
    RTMP推流网关如何实现摄像头微信幼儿园直播?
    国标GB28181流媒体服务器gorm框架内表明重复添加前缀排查
    国标GB28181流媒体平台EasyGBS新版界面视频流更新时间显示错误问题解决
    视频流媒体播放器EasyPlayer-RTSP-Android 如何随意切换播放视频流?
    国标GB28181流媒体服务器分辨率会导致视频无法播放吗?
    国标GB28181流媒体平台集成后播放多个视频部分视频无法播放问题
  • 原文地址:https://www.cnblogs.com/ldxsuanfa/p/10864462.html
  • Copyright © 2011-2022 走看看