读书人

JSON应用总结

发布时间: 2013-08-29 10:33:01 作者: rapoo

JSON使用总结

1、要使用JSON,先下下载json.js文件。下载地址:https://github.com/douglascrockford/JSON-js

定义一个json对象var jsonObject={};给对象属性赋值jsonObject.productId=123;jsonObject.singleType=1;json数组var jsonArray = new Array();jsonArray.push(jsonObject);可以用jquery的each进行循环$.each(jsonArray,function(index,jsonObject){})从json对象获取值var productId=jsonObject.productId;将json对象转换成字符串,用于将值放到隐藏域传到后台var jsonString  = JSON.stringify(jsonObject);{"productId":123,"singleType":1}JSON这个工具下面的两个js工具类提供的,已经在模板上对所有ftl页面进行引用了    如果是转换数组结果为[{"productId":123,"singleType":1},{"productId":456,"singleType":2}]将json格式字符串转换成对象,用于从隐藏域获取value后,转换成对象来操作var jsonObject = JSON.parse('{"productId":123,"singleType":1}');var jsonArray = JSON.parse('[{"productId":123,"singleType":1},{"productId":456,"singleType":2}]');java:隐藏域中的json字符串传到后台后转换成JSONObject对象JSONObject jb = JSONObject.fromObject("{\"productId\":123,\"singleType\":1}");从jsonobject获取属性Long productId = jb.getLong("productId");Double price = jb.getDouble("price");如果jb中没有productId属性,则会抛异常json数组JSONArrayJSONArray js = JSONArray.fromObject("[{\"productId\":123,\"singleType\":1},{\"productId\":456,\"singleType\":2}]");然后循环成JSONObject来获取属性

?

读书人网 >JavaScript

热点推荐