「JSON In Java」ライブラリを以下より入手しています。バージョン→「20211205」リンクの先でFiles→bundleボタンからダウンロードできます。
Just a moment...
package sample;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.stream.Collectors;
import java.util.stream.Stream;
import org.json.JSONArray;
import org.json.JSONObject;
public class JsonAccessOrgJson {
public static void main(String[] args) {
String json_str = null;
Path jsonpath = Paths.get("exec_sql.json");
Stream<String> jsons;
try {
jsons = Files.lines(jsonpath);
json_str = jsons.collect(Collectors.joining(System.lineSeparator()));
//System.out.println(json_str);
jsons.close();
} catch (IOException e1) {
e1.printStackTrace();
}
JSONObject jsonObj = new JSONObject(json_str);
JSONArray items = jsonObj.getJSONArray("colums");
String valuetype = null;
int intvalue ;
String stringvalue ;
for (int i = 0; i < items.length(); i++) {
// JSONオブジェクトをパース
JSONObject item = items.getJSONObject(i);
System.out.printf("%12s:%-12s\n","No.",i);
valuetype = item.get("value_type").toString();
if (valuetype.equals("int") ) {
intvalue = new Integer(item.get("int_value").toString());
System.out.printf("%12s:%-12s\n","intvalue",intvalue);
} else if (valuetype.equals("string") ) {
stringvalue = item.get("string_value").toString();
System.out.printf("%12s:%-12s\n","string_value",stringvalue);
}
System.out.printf("%12s:%-12s\n","db_type",item.get("db_type").toString());
System.out.printf("%12s:%-12s\n","description",item.get("description").toString());
System.out.println("------------");
}
}
}
テストデータ
exec_sql.jsonという名前で以下内容のjsonデータを作成
{
"colums" : [
{
"value_type" : "int"
, "db_type" : "NUMBER"
, "int_value" : 10
, "description" : "備考1"
},
{
"value_type" : "string"
, "db_type" : "VARCHAR2"
, "string_value" : "sooni"
, "description" : "備考2"
}
]
}
実行結果
No.:0
intvalue:10
db_type:NUMBER
description:備考1
------------
No.:1
string_value:sooni
db_type:VARCHAR2
description:備考2
------------
