GSON的简单示例
https://github.com/google/gson
package com.example.wolf;
import com.google.gson.JsonArray;
import com.google.gson.JsonObject;
import com.google.gson.JsonParser;
import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;
import java.util.logging.Level;
import java.util.logging.Logger;
public class GSONTest {
public static void main(String[] args) {
try (BufferedReader br = new BufferedReader(new FileReader("JSONString.txt"))) {
String line;
StringBuilder sb = new StringBuilder();
while ((line = br.readLine()) != null) {
sb.append(line);
}
JsonObject root = new JsonParser().parse(sb.toString()).getAsJsonObject();
JsonArray peopleArray = root.getAsJsonArray("people");
for (int i = 0; i != peopleArray.size(); ++i) {
JsonObject elem = peopleArray.get(i).getAsJsonObject();
String peopleName = elem.get("people_name").getAsString();
String peopleId = elem.get("people_id").getAsString();
String tip = elem.get("tip").getAsString();
System.out.println("people_name: " + peopleName + "\npeople_id: " + peopleId + "\ntip: " + tip);
}
System.out.println();
int peopleCount = root.get("people_count").getAsInt();
String crowdName = root.get("crowd_name").getAsString();
String crowdId = root.get("crowd_id").getAsString();
String tip = root.get("tip").getAsString();
System.out.println("people_count: " + peopleCount + "\ncrowd_name: " + crowdName + "\ncrowd_id: " + crowdId + "\ntip: " + tip + '\n');
} catch (IOException ex) {
Logger.getLogger("JSONTest").log(Level.SEVERE, null, ex);
} finally {
JsonObject jsonObject = new JsonObject();
JsonArray faceArray = new JsonArray();
JsonObject arrayElement = new JsonObject();
JsonObject attributeObject = new JsonObject();
attributeObject.addProperty("age", 19);
attributeObject.addProperty("gender", "Female");
attributeObject.addProperty("lefteye_opendegree", 44);
attributeObject.addProperty("righteye_opendegree", 39);
attributeObject.addProperty("mouth_opendegree", 32);
JsonObject poseObject = new JsonObject();
poseObject.addProperty("raise", -1);
poseObject.addProperty("tilting", -7);
poseObject.addProperty("turn", -3);
attributeObject.add("pose", poseObject);
arrayElement.add("attribute", attributeObject);
arrayElement.addProperty("face_id", "dd6faefeb13a4dcebc14328891bdf6df");
JsonObject positionObject = new JsonObject();
JsonObject centerObject = new JsonObject();
centerObject.addProperty("x", 152);
centerObject.addProperty("y", 92);
positionObject.add("center", centerObject);
JsonObject leftEyeObject = new JsonObject();
leftEyeObject.addProperty("x", 209);
leftEyeObject.addProperty("y", 152);
positionObject.add("eye_left", leftEyeObject);
JsonObject rightEyeObject = new JsonObject();
rightEyeObject.addProperty("x", 312);
rightEyeObject.addProperty("y", 151);
positionObject.add("eye_right", rightEyeObject);
positionObject.addProperty("width", 218);
positionObject.addProperty("height", 218);
arrayElement.add("position", positionObject);
arrayElement.addProperty("tag", "");
faceArray.add(arrayElement);
jsonObject.add("face", faceArray);
jsonObject.addProperty("img_width", 537);
jsonObject.addProperty("img_height", 480);
jsonObject.addProperty("img_id", "020-334630e8948f3403");
jsonObject.addProperty("url", "http://www.eyekey.com/images/demo/BaiBaihe/2.jpg");
System.out.println(jsonObject);
}
}
}
JSONString.txt
{
"people":
[
{
"people_name": "test people",
"people_id": "22fd9efc64c87e00224c33dd8718eec7",
"tip": "people test tip"
}
],
"people_count": 20,
"crowd_name": "test",
"crowd_id": "22fd9efc64c87e00224c33dd8718eec7",
"tip": "test tip"
}
GSON的简单示例的更多相关文章
- retrofit okhttp RxJava bk Gson Lambda 综合示例【配置】
项目地址:https://github.com/baiqiantao/retrofit2_okhttp3_RxJava_butterknife.git <uses-permission andr ...
- Java Json API:Gson使用简单入门
GSON是Google开发的Java API,用于转换Java对象和Json对象.本文讨论并提供了使用API的简单代码示例.更多关于GSON的API可以访问:http://sites.google.c ...
- Linux下的C Socket编程 -- server端的简单示例
Linux下的C Socket编程(三) server端的简单示例 经过前面的client端的学习,我们已经知道了如何创建socket,所以接下来就是去绑定他到具体的一个端口上面去. 绑定socket ...
- C# 构建XML(简单示例)
C# 构建XML的简单示例: var pars = new Dictionary<string, string> { {"url","https://www. ...
- 根据juery CSS点击一个标签弹出一个遮罩层的简单示例
<!DOCTYPE html> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <m ...
- ACEXML解析XML文件——简单示例程序
掌握了ACMXML库解析XML文件的方法后,下面来实现一个比较完整的程序. 定义基本结构 xml文件格式如下 <?xml version="1.0"?> <roo ...
- demo工程的清单文件及activity中api代码简单示例
第一步注册一个账户,并创建一个应用.获取app ID与 app Key. 第二步下载sdk 第三步新建工程,修改清单文件,导入相关的sdk文件及调用相应的api搞定. 3.1 修改清单文件,主要是加入 ...
- spring-servlet.xml简单示例
spring-servlet.xml简单示例 某个项目中的spring-servlet.xml 记下来以后研究用 <!-- springMVC简单配置 --> <?xml versi ...
- SignalR 简单示例
一.什么是 SignalR ASP.NET SignalR is a library for ASP.NET developers that simplifies the process of add ...
随机推荐
- nginx的正则
~ 为区分大小写的匹配. ~* 不区分大小写的匹配(匹配firefox的正则同时匹配FireFox). !~ 不匹配的 !~* 不匹配的 . 匹配除换行符以外的 ...
- OMS数据库调整1
一.增加2T存储空间 1. 对磁盘进行分区并格式化 [root@oms-db01 ~]# fdisk -l Disk /dev/sda: 322.1 GB, 322122547200 bytes 2 ...
- 注册苹果开发者账号和iOS9打包上线
链接: http://www.jianshu.com/p/507ca4e5fde0 http://blog.csdn.net/a283127993/article/details/45828175 i ...
- 刷题总结——保留道路(ssoj)
题目: 题目背景 161114-练习-DAY1-AHSDFZ T3 题目描述 很久很久以前有一个国家,这个国家有 N 个城市,城市由 1,2,3,…,,N 标号,城市间有 M 条双向道路,每条道路都有 ...
- 网页抓取小工具(IE法)
网页抓取小工具(IE法)—— 吴姐 http://club.excelhome.net/thread-1095707-1-1.html 用IE提取网页资料的好处在于:所见即所得,网页上能看到的信息一般 ...
- Ubuntu 常用命令和一些 tips
001. ubuntu 解压.tar.xz文件到另一个文件夹:sudo tar -xvJf ***.tar.xz -C /usr/src sudo 超级用户tar [选项...][file]...-x ...
- LVM 类型的 Storage Pool
LVM 类型的 Storage Pool 不仅一个文件可以分配给客户机作为虚拟磁盘,宿主机上 VG 中的 LV 也可以作为虚拟磁盘分配给虚拟机使用. 不过,LV 由于没有磁盘的 MBR 引导记录,不能 ...
- localStorage增删改查
/** * 设置 本地缓存 */ export function setStorage(key, obj) { if (typeof obj === 'string') { localStorage. ...
- Python入门--13--爬虫一
URL的格式一般为(带方括号的是可选的): protocol://hostname[:port]/path/[;parameters][?query]#fragment URL由三部分组成: 第一部分 ...
- python入门示例程序
该实例是raspi和dsp电机运动控制板的串口uart通信: import serial class SerialHandler(): ''' raspi serial for communicati ...