Json 详解
来源:互联网 发布:js 触发div事件 编辑:程序博客网 时间:2024/06/06 07:06
1、Json
Json起源于1999年的JS语言规范ECMA262的一个子集,后来到了2003年,作为一种新的数据格式ECMA404发布,到了2006年,作为rfc4627发布,这时的规范增加到了18页。
Json的应用非常广泛,截至目前共有100多种语言下的Json库。
2、优点与缺点
2.1、结构
"NAME": "lyl", "AGE": 21, "ID": 5, "BIRTHDAY": "1945-12-12", "SCHOOL": "sunshine school", "SKILL": [ "吃饭", "挖掘机" ], "COMMENT": "this is a comment."}
2.2、优点
2.3、缺点
3、Gson
目前使用最多的也是最好用的Json解析就是Google的Gson了。
你需要去下载最新的Gson的jar包。
下载gson-2.2.2.jar
3.1、Gson的jar包解析
3.2、Gson支持
jar包中主要有以下几个:
3.3、简单的Demo
Java对象转换为Json数据格式的Java对象类
可以看出Gson中添加了许多有意思的东西,对编程的规范有很大的帮助,更详细的就要靠自己发掘了。
package com.vgbh.Bean;import com.google.gson.annotations.SerializedName;public class People { @SerializedName("NAME")//在输出时会将属性的名称进行更换 private String name ; @SerializedName("AGE") private int age ; @SerializedName("ID") private int id ; @SerializedName("BIRTHDAY") private String birthday ; @SerializedName("SCHOOL") private String school ; @SerializedName("SKILL") private String[] skill ; @SerializedName("COMMENT") private String comment ; public String getName() { return name; } public void setName(String name) { this.name = name; } public int getAge() { return age; } public void setAge(int age) { this.age = age; } public int getId() { return id; } public void setId(int id) { this.id = id; } public String getBirthday() { return birthday; } public void setBirthday(String birthday) { this.birthday = birthday; } public String getSchool() { return school; } public void setSchool(String school) { this.school = school; } public String[] getSkill() { return skill; } public void setSkill(String[] skill) { this.skill = skill; } public String getComment() { return comment; } public void setComment(String comment) { this.comment = comment; }}
Demo中含有对toJson()和fromJson()方法的简单实用,
package com.vgbh.gson;import java.io.File;import com.google.gson.Gson;import com.google.gson.GsonBuilder;import com.vgbh.Bean.People;public class GsonTest { public static void main(String[] args) { new GsonTest().createGson(); } public void createGson () { People lyl = new People(); lyl.setName("lyl"); lyl.setAge(21); lyl.setSkill(new String[]{"炒菜", "挖掘机"}); lyl.setBirthday("1996-12-12"); lyl.setComment("this is a comment."); lyl.setId(5); lyl.setSchool("sunshine school"); GsonBuilder builder = new GsonBuilder();//工厂模式 builder.setPrettyPrinting(); Gson gson = builder.create(); System.out.println(gson.toJson(lyl)); } public void analisisGson () { File file = new File(GsonTest.class.getResource("/lyl.json").getFile()); String content = file.toString(); Gson gson = new Gson(); People lyl = gson.fromJson(content, People.class); System.out.println(lyl); }}
有问题的可以联系我的邮箱。
阅读全文
6 0
- JSON 详解
- JSON详解
- JSON详解
- JSON详解
- JSON详解
- JSON详解
- json 详解
- json详解
- JSON详解
- JSON详解
- JSON详解
- json详解
- JSON详解
- JSON详解
- JSON详解
- JSON详解
- json详解
- Json详解
- Linux进程间通信——使用共享内存
- 日夜间模式切换第二种方式
- POJ 1017
- React Native 应用在键盘弹出时优雅地响应
- 欢迎使用CSDN-markdown编辑器
- Json 详解
- netty中自定义协议(加码器和解码器)
- Java基础之日期的调取
- golang配置https协议
- 不要暴力,也可以很容易地解题
- C# DATATABLE 重新排序
- 深入了解Objective-C 和 Core Foundation 对象相互转换(_bridge)
- win7/8/10安装好之后再装Centos7,导致无法开机引导的问题解决.md
- js文件中如何使用EL表达式