JSON与fastjson

来源:互联网 发布:最好的建筑设计软件 编辑:程序博客网 时间:2024/04/30 01:07

JSON(JavaScriptObject Notation)是一种轻量级的数据交换格式。 易于人阅读和编写。同时也易于机器解析和生成。 它基于JavaScript Programming LanguageStandard ECMA-262 3rd Edition - December 1999的一个子集。JSON采用完全独立于语言的文本格式,但是也使用了类似于C语言家族的习惯(包括C, C++, C#, Java,JavaScript, Perl, Python等)。这些特性使JSON成为理想的数据交换语言

JSON建构于两种结构

  • 名称/对的集合(A collection of name/value pairs)。不同的语言中,它被理解为对象(object),纪录(record),结构(struct),字典(dictionary),哈希表(hash table),有键列表(keyed list),或者关联数组 (associative array)。
  • 值的有序列表(An ordered list of values)。在大部分语言中,它被理解为数组(array)。

这些都是常见的数据结构。事实上大部分现代计算机语言都以某种形式支持它们。这使得一种数据格式在同样基于这些结构的编程语言之间交换成为可能。

JSON具有以下这些形式:

对象是一个无序的“‘名称/集合。一个对象以“{”(左括号)开始,“}”(右括号)结束。每个“名称”后跟一个“:”(冒号);“‘名称/值’ 对”之间使用“,”(逗号)分隔。


数组是值(value)的有序集合。一个数组以“[”(左中括号)开始,“]”(右中括号)结束。值之间使用“,”(逗号)分隔。


值(value可以是双引号括起来的字符串(string、数值(number)、true、false、 null、对象(object)或者数组(array)。这些结构可以嵌套。


字符串(string)是由双引号包围的任意数量Unicode字符的集合使用反斜线转义。一个字符(character)即一个单独的字符串(character string)。

字符串(string)与C或者Java的字符串非常相似。


数值(number)也与C或者Java的数值非常相似。除去未曾使用的八进制与十六进制格式。除去一些编码细节。


一个JSON格式的数据示例:

{“Name” : “Apple”,“Expiry” : “2007/10/11 13:54”,“Price” : 3.99,“Sizes” :[“Small”,“Medium”,“Large”]}

在因特网上,JSON正逐渐代替XML成为首选的数据交换格式,这是因为JSON容易阅读并且它的结构匹配常见的编程概念,诸如对象和数组。JSON也比XML更高效(即更快的解析和网络传输),这是因为JSON更紧凑——没有开始和结束标签。

部分内容摘自:http://www.json.org/json-zh.html

 

Fastjson更多示例:http://blog.csdn.net/txtxtxtx2014/article/details/51338343

 

异步加载中使用到JSONObject(其中后台使用到memcache的缓存)

    $.ajax({    url:"unit/get-BKConsumeRanking",    data:{},    type:"post",    dataType:"json",    success:function(data){    if(data.success){    data = data.ranking;    var str ='';     $.each(data, function(i, item) { //遍历list     if(i<=2){         str = str+ '<tr class="zx_tb_row">'+                             '<td class="zx_tb_cell zx_td_1"><img src="http://cdn1.utouu.com/ui/pc/skin/state/images/icon-no'+(i+1)+'.png"></td>'+                             '<td class="zx_tb_cell zx_td_2"><div class="zx_tb_exp zx_tb_name"><img class="rank_photo" src="'+item.photo+'">'+item.name+'</div></td>'+                             '<td class="zx_tb_cell zx_td_3 zx_tb_name">'+item.amount+'</td></tr>';     }else{     str = str+'<tr class="zx_tb_row">'+                             '<td class="zx_tb_cell zx_td_1">'+(i+1)+'</td>'+                             '<td class="zx_tb_cell zx_td_2"><div class="zx_tb_exp zx_tb_name"><img class="rank_photo" src="'+item.photo+'">'+item.name+'</div></td>'+                             '<td class="zx_tb_cell zx_td_3 zx_tb_name">'+item.amount+'</td></tr>';     }            });    $(".BKConsumeRanking").append(str);    }    }    })

/** * 查询府消费排行 * @since 1.1 * @param request r * @return * <br><b>作者: @author tengxiang</b> * <br>创建时间:2016年4月18日 上午11:12:08 */@ResponseBody@RequestMapping(value = "/get-BKConsumeRanking")public JSONObject getBKConsumeRanking(HttpServletRequest request){HttpSession session = request.getSession();Long unitId = null;if (session.getAttribute(Constants.NMS_SESSION_DEFFAULT_UNIT) == null) {throw new BusinessException("非法请求!");}JSONObject json = new JSONObject();json.put("success", false);UnitDto u = (UnitDto) session.getAttribute(Constants.NMS_SESSION_DEFFAULT_UNIT);unitId = u.getId();ArrayList<UserBKConsumeRanking> list = new ArrayList<UserBKConsumeRanking>();String key = MemcacheKeys.F_BKCONSUME_RANKING_UNIT+unitId;Object object = memcachedClient.get(key);if (object != null) {list = (ArrayList<UserBKConsumeRanking>) object;json.put("success", true);json.put("ranking", list);return json;}//获取府消费排行String bkConsumeRanking = bestkeepService.getBKConsumeRanking(unitId, null,null);HashMap<String,ArrayList<UserBKConsumeRanking>> jsonMap = JSON.parseObject(bkConsumeRanking, new TypeReference<HashMap<String,ArrayList<UserBKConsumeRanking>>>() {});list = jsonMap.get("data");if (list != null && !list.isEmpty()) {List<Long> userIds = new ArrayList<Long>();for(UserBKConsumeRanking userinfo :list){userIds.add(userinfo.getUser_id());}List<UserExpandDto> expandDtos = new ArrayList<UserExpandDto>();if (userIds != null && userIds.size() > 0) {expandDtos = userExpandService.getUsersByUserIds(userIds);}for (UserExpandDto userExpandDto : expandDtos) {for(UserBKConsumeRanking userinfo :list){if (userinfo.getUser_id().equals(userExpandDto.getUserId())) {userinfo.setName(userExpandDto.getName());userinfo.setPhoto(NmsUtils.photo(userPhotoUrl, userExpandDto.getPhoto()));}}}}int size = 6-list.size(); for(int i=0;i < size;i++){String str = "{\"amount\":\"----\",\"user_id\":\"\",\"name\":\"未被占领\"}";UserBKConsumeRanking obj = JSON.parseObject(str, new TypeReference<UserBKConsumeRanking>() {});list.add(obj);}memcachedClient.set(key, MemcacheKeys.MEM_CACHE_TIME_ONE_HOUR, list);json.put("success", true);json.put("ranking", list);return json;}



0 0
原创粉丝点击