使用gson解析,生成Json
包:gson-2.3.jar
Json文本解析为Java对象;Java对象生成为Json文本

import com.google.gson.Gson;
public class TestGson {
class Person {
private String name;
private int[] power;
private Horse hs1;
private Horse[] hs2;
public Horse getHs1() {
return hs1;
}
public void setHs1(Horse hs1) {
this.hs1 = hs1;
}
public Horse[] getHs2() {
return hs2;
}
public void setHs2(Horse[] hs2) {
this.hs2 = hs2;
}
public int[] getPower() {
return power;
}
public void setPower(int[] power) {
this.power = power;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
}
class Horse {
private String type;
public String getType() {
return type;
}
public void setType(String type) {
this.type = type;
}
}
public static void main(String[] args) {
String strJson = "{'name':'吕奉先','power':[100,30],'hs1':{'type':'赤兔'},'hs2':[{'type':'乌骓'},{'type':'黄标'}]}";
Gson g = new Gson();
// Json->对象
Person p = g.fromJson(strJson, Person.class);
System.out.println(p.name + ":" + p.power[1]);
//对象->Json
p.setName("关羽");
String json = g.toJson(p);
System.out.println(json);
}
}
使用gson解析,生成Json的更多相关文章
- PHP获取cookie、Token、模拟登录、抓取数据、解析生成json
本文介绍使用PHP获取cookie,获取Token.以及模拟登录.然后抓取数据.最后解析生成json的的过程. 0. 设置Cookie路径 set_time_limit(0); //使用的cookie ...
- Gson解析复杂Json数据
背景 json是一种数据格式,便于数据传输.存储.交换. gson是 ...
- 使用jsoncpp解析生成json
在此站点下载jsoncpp(https://sourceforge.net/projects/jsoncpp/这个站点的版本较旧) 在电脑上安装Python,运行amalgamate.py,生成的di ...
- C#.Net 使用 JsonReader/JsonWriter 高性能解析/生成 Json 文档
Swifter.Json 是由本人编写的高性能且多功能的 Json 解析库.下图是 Swifter.Json 与 .Net 平台上的其他 Json 库性能对比: 在 Swifter.Json 近期更新 ...
- 使用GSON来生成JSON数据
第二种方法: 当不需要显示某个属性时,在不需要显示出的属性前加transient关键字即可满足 使用gson来解析 使用gson解析 带日期转换 集合类解析:gson中的数组与java中集合类都是对应 ...
- Gson解析复杂JSON对象
例如以下格式JSON: 建立对应的Java对象,注意内部类要定义成静态的 public class HResult { public String total; public String recor ...
- Gson解析复杂JSON字符串的两种方式
JSON解析可以使用的库: JSONObject(源自Android官方). Gson(源自Google). Jackson(第三方开源库). FastJSON(第三方开源库). 本文例子使用Goog ...
- java android使用Gson解析泛型json数据
那就直接开始吧. 在我们获取服务器返回的json数据有时候会出现这种情况,比如: {"body":{"attrName":"feed",&q ...
- Android Gson解析复杂Json
JSON原数据 {"total":1,"rows":[{"ID":1,"Title":"台州初级中学招收初一年 ...
- Gson解析纯Json数组
[ { "type": "123", "value": 123 }, { "type": "234" ...
随机推荐
- 通过WebViewJavascriptBridge实现OC与JS交互
在.m方法当中,申明一个WebViewJavascriptBridge属性: @interface ExampleAppViewController () @property WebViewJav ...
- 手把手教你用python抓网页数据
http://www.1point3acres.com/bbs/thread-83337-1-1.html
- ubuntu fix the grub boot(need Internet)
sudo add-apt-repository ppa:yannubuntu/boot-repair sudo apt-get update sudo apt-get install -y boot- ...
- Markov Random Fields
We have seen that directed graphical models specify a factorization of the joint distribution over a ...
- (转)深入理解JavaScript 模块模式
深入理解JavaScript 模块模式 (原文)http://www.cnblogs.com/starweb/archive/2013/02/17/2914023.html 英文:http://www ...
- 进程间通信IPC:消息队列,信号量,共享内存
2015.3.4星期三 阴天 进程间通信:IPC 文件对象:记录文件描述符,文件开关等 IPC标示符:系统全局的流水号两个进程要通信,打开的是唯一的对象进行通讯,通过key操作 XSI IPC:消息队 ...
- Zyxel Switch-How to block a fake DHCP server without enabling DHCP snooping?
How to block a fake DHCP server without enabling DHCP snooping? Scenario How to block a fake DHCP se ...
- 大数据批量插入数据库使用(SqlBulkCopy )效率更高
SqlBulkCopy类是System.Data.SqlClient下的类,我们开发中不常用,甚至不知道有这么一个类的存在,但确实比sql插入,事务批量插入,sql批量拼接插入快很多,比调用存储过程插 ...
- Git 一次性 pull push 所有的分支
/********************************************************************************* * Git 一次性 pull pu ...
- [转]设置Android手机以使用ARM Streamline进行性能分析(二)
原文因为arm社区改版访问不到了,原作者鲍方,原文地址,这篇是从google cache里挖出来的,希望能帮到要对cocos2dx优化的各位 Posted by Fang Bao, Leave C ...