json对象操作
来源:互联网 发布:淘宝店铺活动如何取消 编辑:程序博客网 时间:2024/06/06 13:00
1.访问json数据
var persons = {
"programmers":[
{"firstname":"yang","lastname":"rui","email","384183958@qq.com"},
{"firstname":"yang","lastname":"rui","email","384183958@qq.com"},
{"firstname":"yang","lastname":"rui","email","384183958@qq.com"},
],
"authors":[
{"firstname":"yang","lastname":"rui","email","384183958@qq.com"},
{"firstname":"yang","lastname":"rui","email","384183958@qq.com"},
{"firstname":"yang","lastname":"rui","email","384183958@qq.com"},
],
"musicians":[
{"firstname":"yang","lastname":"rui","email","384183958@qq.com"},
{"firstname":"yang","lastname":"rui","email","384183958@qq.com"},
{"firstname":"yang","lastname":"rui","email","384183958@qq.com"},
],
}
访问:persons .programmers[0].firstname
修改:persons .programmers[0].firstname="jia"
转化:任何javascript对象都可以转换成json字符串 toJSONString()
2.Ajax
json可以轻松地将数据转换为可以在请求中发送给服务器端程序的格式(Ajax)。
1、通过Json格式来声明对象和操作对象
function showJSON()
{
var user =
{
"username":"andy",
"age":20,
"info": { "tel": "123456", "cellphone": "98765"},
"addresses":
[
{"city":"beijing","postcode":"222333"},
{"city":"newyork","postcode":"555666"}
]
}
alert(user.username);
alert(user.age);
alert(user.info.cellphone);
alert(user.address[0].city);
alert(user.address[0].postcode);
}
这表示一个user对象,拥有username, age, info, address 等属性。
2、一般对象转换成JSON格式的字符
JSON提供了json.js包,下载http://www.json.org/json.js 后,将其引入然后就可以简单的使用object.toJSONString()转换成JSON数据。
js 代码
function Car(make, model, year, color) {
this.make = make;
this.model = model;
this.year = year;
this.color = color;
}
function showCar() {
var carr = new Car("Dodge", "Coronet R/T", 1968, "yellow");
alert(carr.toJSONString());
}
js对象和JSON格式的js对象、JSON格式的js对象和JSON文本的转换
可以使用eval来转换JSON字符到JSON格式的Object
js 代码
function myEval() {
var str = '{ "name": "张三", "sex": "男" }';
var obj = eval('(' + str + ')');
alert(obj.toJSONString());
}
或者使用parseJSON()方法
function myparse()
{
var str= '{ "name": "张三", "sex": "男" }';
var obj=str.parseJSON(); //把其Json文本解析成为json对象
alert(obj.toJSONString()); //把json对象转换成JSON 文本
}
3、从Java 类产生json对象(json-lib)
(1)、JSONObject
//java对象或满足一定格式的字符串("{username:'admin',password:'12345',..}")转化为JSONObject对象,用法:
JSONObject json=JSONObject.fromObject(userinfo);
//从json对象中根据键名取对应的值, username为键名
json.getObject("username");
//json对象到java对象的转化(jsonobject-->po)
Userinfo userinfo=(Dept)JSONObject.toBean(jsonObject,Userinfo.class);
//Json对象转化成json文本字符串
String josStr=JSONObject.fromObject(userinfo).toString();
(2)、JSONArray
boolean[] boolArray={true,false};
List list = new ArrayList();
Map map=new HashMap();
object[] test={new Userinfo(),new Userinfo()};
//转化为java的数组Array为json对象
JSONArray jsonArray = JSONArray.fromObject(boolArray);
//System.out.println(jsonArray.toString());
JSONArray jsonArray1=JSONArray.fromObject("['1','2','3']");
//System.out.println(jsonArray1.toString());
list.add("first");
list.add("second");
list.add(new Userinfo());
//转换java的List为json对象
JSONArray jsonList = JSONArray.fromObject(list);
System.out.println(jsonList);
//转换java的Map为json对象
map.put("name", "windfree");
map.put("bool", Boolean.TRUE);
map.put("int",new Integer(0));
map.put( "arr", new String[]{"a","b"} );
map.put("func", "function (i){ return this.arr[i]; }");
map.put("bean",test);
JSONObject jsonMap = JSONObject.fromObject(map);
//System.out.println(jsonMap);
- JSON对象操作一
- JS操作JSON对象
- 操作JSON对象
- python操作json对象
- JSON对象操作小结
- js操作json对象
- json对象操作
- jquery操作json对象
- JAVA操作json对象--json.jar
- json的增删改(json操作对象)
- JavaScript操作json对象,json字符串
- json 处理得到 json对象 操作JSONObject
- 根据属性操作JSON对象
- JSON对象互相转换操作
- JS操作JSON对象总结
- c/c++ json对象操作
- 【json】json对象和json数组的操作
- JSON格式字符串转对象、对象转JSON操作类
- 神经网络机器翻译模型介绍-GNMT
- 双向级联中的一对多导航
- HashMap类源码解析
- tomcat启动成功并能运行项目,但无法显示那只小猫
- 运用反射从bean对象中取出属性 页面显示list优化
- json对象操作
- 【工作笔记】使用mescroll.js实现上拉加载更多
- 从底层驱动 到上层APP的流程(1)
- Oracle之获取真正的执行计划
- mtd-utils工具的编译和使用
- FE
- STM32之红外接收
- AOP的底层实现概念
- Yarn的Memory和CPU调优配置详解