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" }