[英]Dynamic way to access JSON nested values in Java
我有这个JSON对象:
{
"maindrawer":
{
"enabled": true,
"actions":
[
{
"type": "Section",
"title": "Section 1"
},
{
"id": 1,
"type": "Primary",
"title": "Title 1",
"badge":
{
"enabled": false,
"value": 0,
"textColor": "#000000",
"badgeColor": "#ff0990"
},
"subActions":
[
{
"id": 1,
"type": "Primary",
"title": "Sub Title 1"
}
]
}
]
}
}
这是我用来访问徽章的代码 - > textColor值:
public void loadJSONFromRaw(Context context, int id)
{
json = null;
try
{
//read and return json sting
InputStream is = context.getResources().openRawResource(id);
int size = is.available();
byte[] buffer = new byte[size];
is.read(buffer);
is.close();
json = new String(buffer, "UTF-8");
//convert json to object
Type type = new TypeToken<Map<String, Object>>() {}.getType();
Map<String, Object> data = new Gson().fromJson(json, type);
//access maindrawer property
Map<String, Object> maindrawer = (Map<String, Object>)data.get("maindrawer");
//access actions list
List<Object> actions = (List<Object>)maindrawer.get("actions");
//return first item in the list
Map<String, Object> action = (Map<String, Object>) actions.get(1);
//return badge object
Map<String, String> badge = (Map<String, String>) action.get("badge");
//access badge -> textColor value
String textColor = badge.get("textColor");
}
catch (IOException e)
{
e.printStackTrace();
}
}
是否有更好/更快或更动态的方式来使用java / android访问JSON嵌套属性? 我正在使用Gson库来执行此任务,并且不介意切换到任何其他解决方案以使其更容易,因为这只是为了访问单个变量而编写的代码太多。
理想情况下,我正在寻找类似的东西:
String textColor = data.get("maindrawer").get("actions").get(1).get("badge").get("textColor");
此外,我对使用POJO不是很感兴趣。
最后,我还是Java的新手,所以我可能在这里遗漏了一些东西,或者有一些限制? 无论如何,谢谢你的帮助!!
由于您在本地访问json文件,这意味着您知道它的结构。 所以不要使用 - Map<String, Object> data = new Gson().fromJson(json, type);
你可以使用这样的东西 -
Map<String, MainDrawer> data = new Gson().fromJson(json, type);
其中MainDrawer
是一个具有成员变量的类 - enabled
, actions
和另一种类型的数组。
这将更容易获取您的值,如使用 - mainDrawer.isEnabled()
这里有两个解决方案,无需导入新库。
编写一个简单的路径解析器:
String textColor = (String)parse(data, "maindrawer", "actions", 1, "badge", "textColor");
//...
static Object parse(Object root, Object... params) {
Object current = root;
for (Object p : params) {
if (p instanceof Number) {
current = ((List<?>)current).get(((Number)p).intValue());
} else {
current = ((Map<?,?>)current).get(p.toString());
}
}
return current;
}
或者解析并浏览Gson的JsonElement
:
JsonElement root = new Gson().fromJson(json, JsonElement.class);
String textColor = root
.getAsJsonObject().get("maindrawer")
.getAsJsonObject().get("actions")
.getAsJsonArray().get(1)
.getAsJsonObject().get("badge")
.getAsJsonObject().get("textColor")
.getAsString();
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.