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 ...
随机推荐
- BASH重定向问题
APUE 3.5关于重定向有个容易迷惑人的问题: ./a.out > outfile 2>&1 ./a.out 2>&1 > outfile 问两者区别? in ...
- shell的until循环
until 循环执行一系列命令直至条件为 true 时停止.until 循环与 while 循环在处理方式上刚好相反.一般while循环优于until循环,但在某些时候,也只是极少数情况下,until ...
- PAT天梯赛练习题——L3-003. 社交集群(并查集按秩合并)
L3-003. 社交集群 时间限制 1000 ms 内存限制 65536 kB 代码长度限制 8000 B 判题程序 Standard 作者 陈越 在社交网络平台注册时,用户通常会输入自己的兴趣爱好, ...
- 算法复习——高斯消元(ssoi)
题目: 题目描述 Tom 是个品学兼优的好学生,但由于智商问题,算术学得不是很好,尤其是在解方程这个方面.虽然他解决 2x=2 这样的方程游刃有余,但是对于下面这样的方程组就束手无策了.x+y=3x- ...
- cf660E Different Subsets For All Tuples
For a sequence a of n integers between 1 and m, inclusive, denote f(a) as the number of distinct sub ...
- Aragorn's Story(hdu3966)
题意:给一棵树,并给定各个点权的值,然后有3种操作: I C1 C2 K: 把C1与C2的路径上的所有点权值加上K D C1 C2 K:把C1与C2的路径上的所有点权值减去K Q C:查询节点编号为C ...
- Python入门--4--分之和循环
1.用ELIF比较省CPU: 第一种方法,使用if score = int(input('请输入你的分数:')) if (score <= 100) and (score >= 90): ...
- Java学习:一 开篇
呃 工作中要用到Android开发,呃 不巧的是,关于Java关于Android,当初也只是浅浅的了解了一下.....真是书到用时方恨少了.. 趁现在工作不是太忙,还是花点时间来学习一下吧. 写写博客 ...
- elasticsearch入门使用(三) Query DSL
Elasticsearch Reference [6.2] » Query DSL 参考官方文档 :https://www.elastic.co/guide/en/elasticsearch/refe ...
- noip2009提高组解题报告
NOIP2009潜伏者 题目描述 R 国和S 国正陷入战火之中,双方都互派间谍,潜入对方内部,伺机行动. 历尽艰险后,潜伏于 S 国的R 国间谍小C 终于摸清了S 国军用密码的编码规则: 1. S 国 ...