java開発でjsonを扱うサンプルソース
コード下記
Object getJsonField(JSONObject root, String path) {
Pattern array = Pattern.compile(“(.*)\\[(\\d+)\\]");
Object retback = root;
for(String part : path.split(“\\.")){
Matcher matcher = array.matcher(part);
if(matcher.matches()){
String arrayName = matcher.group(1);
if(!arrayName.isEmpty()){
retback = ((JSONObject)retback).get(arrayName);
}
int index = Integer.parseInt(matcher.group(2));
if(retback != null){
retback = ((JSONArray)retback).get(index);
}
}else{
retback = ((JSONObject)retback).get(part);
}
if(retback == null){
break;
}
}
return retback;
}