json

作者: 无聊新生 | 来源:发表于2017-08-21 19:14 被阅读0次

json

概念:javascript object notation js标示对象的一种方式

作用: 1、标示数据,键值对形式 2、保存数据 3、json可以作为数据交换的格式

特点:轻量级,原生js

json对象 本质就是js代码 var aa={"name":"lisu"}

json数据 字符串

eval();

json数据

Map形式

  1. key/value
{"name":"zhangsan","age":"30"};
  1. object
{"stu":{"name":"lisi","age":"21"}}
  1. array
{"tea":[
        {"name":"lisi","age":"21"},
        {"name":"zhangsan","age":"22"}
    ]
}
  1. 复杂嵌套
{
    "stu2":[
        {"sex":0,"username":"lisi"},{"sex":0,"username":"zhaoliu"},
        {"password":"123","sex":0,"username":"zhangsan"}
    ],
    "stu1":{"password":"123","sex":1,"username":"zhangsan"}
}
  1. List形式
[
    {"name":"lisi","age":"21"},
    {"name":"zhangsan","age":"22"}
]
[
    [
        {"name":"lisi","age":"21"},
        {"name":"zhangsan","age":"22"}
    ],
    [
        {"name":"lisi","age":"21"},
        {"name":"zhangsan","age":"22"}
    ]
]

json工具类

  1. 访问官网http://www.json.org/

  2. 找到java平台的json工具类,例如json-java

  3. 到github下载源码

  4. 导入到eclipse,导出jar

json-java的使用

User user=new User("zhaoliu", "123");
JSONObject json=new JSONObject(user);//将user对象转成json对象
  • list对象转成json对象
User user1=new User("lisi","123");
User user2=new User("zhaoliu","123");
User user3=new User("zhangsan","123");

List<User> list=new ArrayList<User>();
list.add(user1);
list.add(user2);
list.add(user3);

JSONArray json=new JSONArray(list);//将list<User>转成json对象

相关文章

网友评论

      本文标题:json

      本文链接:https://www.haomeiwen.com/subject/uqjhdxtx.html