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. 如何解决 错误code signing is required for product type 'xxxxx' in SDK 'iOS 8.2'

    如何解决 错误code signing is required for product type 'xxxxx' in SDK 'iOS 8.2' 大家在做真机调试的时候,或许会遇到这样的问题,那如何 ...

  2. POJ2167 Irrelevant Elements

    Time Limit: 5000MS   Memory Limit: 65536KB   64bit IO Format: %lld & %llu Description Young cryp ...

  3. python模块(二)

    一.json模块 作用: 用于[字符串]和 [python基本数据类型] 间进行转换 Python的Json模块序列化与反序列化的过程分别是 encoding和 decoding. encoding ...

  4. Arduino学习笔记1---开发环境搭建

    主要内容:(一). Arduino IDE的下载及安装 (二). Arduino IDE的应用 (三). Arduino的程序结构 (四). Arduino程序的编译及下载 (一). Arduino ...

  5. 【转】UITableViewCell自适应高度 UILabel自适应高度和自动换行

    - (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {     ...

  6. jquery知识汇总

    jQuery 选择器 选择器                  实例                                   选取 *                          $ ...

  7. golang文件下载断点续传(下载客户端)

    客户端: //const ( // UA = "Golang Downloader from Kejibo.com" //) func DownloadController(ctx ...

  8. SGU 105 数学找规律

    观察一下序列,每3个数一组,第一个数余1,不能,加第二个数后整除(第二个数本身余2),第三数恰整除.一行代码的事.011011011.... #include<iostream> usin ...

  9. AC日记——砝码称重 洛谷 P2347

    题目描述 设有1g.2g.3g.5g.10g.20g的砝码各若干枚(其总重<=1000), 输入输出格式 输入格式: 输入方式:a1 a2 a3 a4 a5 a6 (表示1g砝码有a1个,2g砝 ...

  10. 如何选择 IT 技术书籍

    ★第1招:看网上评论 首先,上一些权威的图书网站,看看大伙儿的评价如何(要相信群众的眼睛是雪亮的).对于英文书籍,我一般上亚马逊网站去看看:中文书籍则上豆瓣网.这两个网站都提供星级评分,一般 > ...