yipuran-gsonhelper/JSonValue.java at master · yipuran/yipuran-gsonhelper · GitHub
を使用して、プリントする
import com.google.gson.Gson; import com.google.gson.GsonBuilder; import org.yipuran.gsonhelper.JSonValue;
try(InputStream in = new FileInputStream("sample.json"); ByteArrayOutputStream bo = new ByteArrayOutputStream()){ JSonValue jsonvalue = new JSonValue(new InputStreamReader(in, StandardCharsets.UTF_8)); jsonvalue je = jsonvalue.get("aaa.bbb.ccc"); try(ByteArrayOutputStream bo = new ByteArrayOutputStream()){ JsonWriter writer = new JsonWriter(new PrintWriter(bo)); Gson gson = new GsonBuilder().serializeNulls().setPrettyPrinting().create(); gson.toJson(je, writer); writer.flush(); // JSON path "aaa.bbb.ccc" が指すオブジェクトを文字列化 String result = bo.toString(); }catch(IOException e){ e.printStackTrace(); } }catch(Exception e){ e.printStackTrace(); }
目的の String result を求めることができる。