包: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的更多相关文章

  1. PHP获取cookie、Token、模拟登录、抓取数据、解析生成json

    本文介绍使用PHP获取cookie,获取Token.以及模拟登录.然后抓取数据.最后解析生成json的的过程. 0. 设置Cookie路径 set_time_limit(0); //使用的cookie ...

  2. Gson解析复杂Json数据

    背景                                                                   json是一种数据格式,便于数据传输.存储.交换. gson是 ...

  3. 使用jsoncpp解析生成json

    在此站点下载jsoncpp(https://sourceforge.net/projects/jsoncpp/这个站点的版本较旧) 在电脑上安装Python,运行amalgamate.py,生成的di ...

  4. C#.Net 使用 JsonReader/JsonWriter 高性能解析/生成 Json 文档

    Swifter.Json 是由本人编写的高性能且多功能的 Json 解析库.下图是 Swifter.Json 与 .Net 平台上的其他 Json 库性能对比: 在 Swifter.Json 近期更新 ...

  5. 使用GSON来生成JSON数据

    第二种方法: 当不需要显示某个属性时,在不需要显示出的属性前加transient关键字即可满足 使用gson来解析 使用gson解析 带日期转换 集合类解析:gson中的数组与java中集合类都是对应 ...

  6. Gson解析复杂JSON对象

    例如以下格式JSON: 建立对应的Java对象,注意内部类要定义成静态的 public class HResult { public String total; public String recor ...

  7. Gson解析复杂JSON字符串的两种方式

    JSON解析可以使用的库: JSONObject(源自Android官方). Gson(源自Google). Jackson(第三方开源库). FastJSON(第三方开源库). 本文例子使用Goog ...

  8. java android使用Gson解析泛型json数据

    那就直接开始吧. 在我们获取服务器返回的json数据有时候会出现这种情况,比如: {"body":{"attrName":"feed",&q ...

  9. Android Gson解析复杂Json

    JSON原数据 {"total":1,"rows":[{"ID":1,"Title":"台州初级中学招收初一年 ...

  10. Gson解析纯Json数组

    [ { "type": "123", "value": 123 }, { "type": "234" ...

随机推荐

  1. oracle生成行方法

    数据库记录是行的集合 set of row, 那么如何如何生成集合呢? oracle中常用的是 递归查询(with ... union all ...) 以及 connect by(树形查询) htt ...

  2. 标准IO的简单应用,动静态库,读取系统时间并打印,模拟ls -l功能

    2015.2.27星期五,小雨 标准IO实现的复制功能: #include <stdio.h>#include <errno.h> #define N 64 int main( ...

  3. Android RecyclerView单击、长按事件:基于OnItemTouchListener +GestureDetector标准实现(二),封装抽取成通用工具类

     Android RecyclerView单击.长按事件:基于OnItemTouchListener +GestureDetector标准实现(二),封装抽取成通用工具类 我写的附录文章2,介绍了 ...

  4. 1、JS的数据类型

    一.分类:JS有6种数据类型: 1.string 2.undefined 3.number 4.null 5.boolean 6.object(包含函数.数组.日期等) 二.隐式转换 1.+和- va ...

  5. HTML5实战——svg学习

    百度百科: SVG可缩放矢量图形(Scalable Vector Graphics)是基于可扩展标记语言(XML),用于描述二维矢量图形的一种图形格式.SVG是W3C制定的一种新的二维矢量图形格式,也 ...

  6. select 通过jq赋值

    <select name="F_YSBAQLX" onchange="selectvalue()" id="lista" prompt ...

  7. 一些相关的github

    https://github.com/LTS4/DeepFool 貌似是说可以愚弄深度神经网络? https://github.com/tflearn/tflearn TF学习指南 http://gi ...

  8. C++ Pirmer : 第十五章 : 面向对象程序设计之基类和派生的定义、类型转换与继承与虚函数

    基类和派生类的定义以及虚函数 基类Quote的定义: classs Quote { public: Quote() = default; Quote(cosnt std::string& bo ...

  9. ✡ leetcode 163. Missing Ranges 找出缺失范围 --------- java

    Given a sorted integer array where the range of elements are in the inclusive range [lower, upper], ...

  10. Codeforces Round #158 (Div. 2)

    A. Adding Digits 枚举. B. Ancient Prophesy 字符串处理. C. Balls and Boxes 枚举起始位置\(i\),显然\(a_i \le a_j, 1 \l ...