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的简单示例的更多相关文章

  1. retrofit okhttp RxJava bk Gson Lambda 综合示例【配置】

    项目地址:https://github.com/baiqiantao/retrofit2_okhttp3_RxJava_butterknife.git <uses-permission andr ...

  2. Java Json API:Gson使用简单入门

    GSON是Google开发的Java API,用于转换Java对象和Json对象.本文讨论并提供了使用API的简单代码示例.更多关于GSON的API可以访问:http://sites.google.c ...

  3. Linux下的C Socket编程 -- server端的简单示例

    Linux下的C Socket编程(三) server端的简单示例 经过前面的client端的学习,我们已经知道了如何创建socket,所以接下来就是去绑定他到具体的一个端口上面去. 绑定socket ...

  4. C# 构建XML(简单示例)

    C# 构建XML的简单示例: var pars = new Dictionary<string, string> { {"url","https://www. ...

  5. 根据juery CSS点击一个标签弹出一个遮罩层的简单示例

    <!DOCTYPE html> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <m ...

  6. ACEXML解析XML文件——简单示例程序

    掌握了ACMXML库解析XML文件的方法后,下面来实现一个比较完整的程序. 定义基本结构 xml文件格式如下 <?xml version="1.0"?> <roo ...

  7. demo工程的清单文件及activity中api代码简单示例

    第一步注册一个账户,并创建一个应用.获取app ID与 app Key. 第二步下载sdk 第三步新建工程,修改清单文件,导入相关的sdk文件及调用相应的api搞定. 3.1 修改清单文件,主要是加入 ...

  8. spring-servlet.xml简单示例

    spring-servlet.xml简单示例 某个项目中的spring-servlet.xml 记下来以后研究用 <!-- springMVC简单配置 --> <?xml versi ...

  9. SignalR 简单示例

    一.什么是 SignalR ASP.NET SignalR is a library for ASP.NET developers that simplifies the process of add ...

随机推荐

  1. shell的格式化输出命令printf

    printf 命令用于格式化输出, 是echo命令的增强版.它是C语言printf()库函数的一个有限的变形,并且在语法上有些不同. 注意:printf 由 POSIX 标准所定义,移植性要比 ech ...

  2. Unity3D for iOS初级教程:Part 2/3

    转自Unity3D for iOS 这篇文章还可以在这里找到 英语 Learn how to use Unity to make a simple 3D iOS game! 这篇教材是来自教程团队成员 ...

  3. 常州模拟赛d3t2 灰狼呼唤着同胞

    题目背景 我的母亲柯蒂丽亚,是一个舞者.身披罗纱,一身异国装扮的她,来自灰狼的村子. 曾经在灰狼村子担任女侍的她,被认定在某晚犯下可怕的罪行之后,被赶出了村子. 一切的元凶,都要回到母亲犯下重罪的那一 ...

  4. 美丽的大树(codevs 2124)

    题目描述 Description 平江路是苏州最美丽的道路,路中间的绿化带上种了两行漂亮的大树,每行50棵,一共100棵大树,这些大树被编上了号,编号方式如下: 1 3 5 7 ………… 45 47 ...

  5. HDU4768:Flyer [ 二分的奇妙应用 好题 ]

    传送门 Flyer Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total S ...

  6. php生成压缩包

    $filename = "./" . date ( 'YmdH' ) . ".zip"; // 最终生成的文件名(含路径) // 生成文件 $zip = new ...

  7. Java学习:一 开篇

    呃 工作中要用到Android开发,呃 不巧的是,关于Java关于Android,当初也只是浅浅的了解了一下.....真是书到用时方恨少了.. 趁现在工作不是太忙,还是花点时间来学习一下吧. 写写博客 ...

  8. Codeforces 375 D Tree and Queries

    Discription You have a rooted tree consisting of n vertices. Each vertex of the tree has some color. ...

  9. Nginx的Web管理界面收集

    Nginx实在是太强大了!灰度发布.金丝雀发布.负载均衡就只需要简单的几行配置就可以实现,这些特性嗾使微软无法比拟的. 原来Nginx除了使用ngx_http_stub_status_module模块 ...

  10. 春哥的nginx systemtap调试脚本简单介绍

    http://card.weibo.com/article/h5/s#cid=2304185311ad2d0102v9gd&vid=0&extparam=&from=11100 ...