英文:
Read hashmap nested properties dynamically
问题
我有以下JSON:
"total":"2",
"offset":"1",
"limit":"2",
"results":[{
"code":1,
"title":"RESTAURANTE SADOCHE",
"contact":{
"code":10,
"name":"HENRIQUE BARBALHO",
"company":{
"code":100,
"name":"RESTAURANTE SADOCHE LTDA-ME"
}
}
},
{
"code":2,
"title":"ARNALDO GRILL",
"contact":{
"code":20,
"name":"FÁTIMA COSTA",
"company":{
"code":200,
"name":"COSTA NATAL RESTAURANTE EIRELI"
}
}
}]
我使用Gson库将此JSON转换为Java HashMap:
Map<String, Object> retMap = new Gson().fromJson(jsonUpString, new TypeToken<HashMap<String, Object>>(){}.getType());
我需要动态读取此创建的哈希映射的某些属性。例如:标题,联系人的姓名和公司的名称。
有时,这些属性(标题,联系人的姓名和公司的名称)可能在列表中。
以下是我的代码:
String propertyName = "name";
String nesting = "results;contact;company";
String[] levels = nesting.split(";");
Map map = new HashMap();
map = retMap;
for (int i = 0; i < niveis.length; i++) {
map = (Map)map.get(levels[i]);
System.out.println(map);
if (i == levels.length - 1) {
System.out.println(map.get(propertyName));
}
}
但是,如果属性(results,contact或company)返回多个对象,JSON会将它们作为列表返回,我无法获取所需的信息。
英文:
I have the JSON below:
"total":"2",
"offset":"1",
"limit":"2",
"results":[{
"code":1,
"title":"RESTAURANTE SADOCHE",
"contact":{
"code":10,
"name":"HENRIQUE BARBALHO",
"company":{
"code":100,
"name":"RESTAURANTE SADOCHE LTDA-ME"
}
}
},
{
"code":2,
"title":"ARNALDO GRILL",
"contact":{
"code":20,
"name":"FÁTIMA COSTA",
"company":{
"code":200,
"name":"COSTA NATAL RESTAURANTE EIRELI"
}
}
}]
I turned this JSON into a Java HashMap using the Gson library.
Map<String, Object> retMap = new Gson().fromJson(jsonUpString, new TypeToken<HashMap<String, Object>>(){}.getType());
I need to dynamically read some properties of this created hashmap. Ex: title, name of contact and name of company.
Sometimes these properties (title, name of contact and name of company) can be inside lists.
Below my code:
String propertyName = "name";
String nesting = "results;contact;company";
String[] levels = nesting.split(";");
Map map = new HashMap();
map = retMap;
for (int i = 0; i < niveis.length; i++) {
map = (Map)map.get(levels[i]);
System.out.println(map);
if (i == levels.length - 1) {
System.out.println(map.get(propertyName));
}
}
But if the properties (results, contact or company) return more than one object, the JSON returns them as lists, and I can't get the information I need.
答案1
得分: 0
我使用以下方式解决了这个问题...
private static void leJSON(Object object) {
if (object instanceof JSONObject) {
Set<String> ks = ((JSONObject) object).keySet();
for (String key : ks) {
Object value = ((JSONObject) object).get(key);
if (value != null) {
System.out.printf("%s=%s (%s)\n", key, value, value.getClass().getSimpleName());
if (value.getClass().getSimpleName().equalsIgnoreCase("JSONArray")) {
JSONArray ja = (JSONArray) value;
for (int i = 0; i < ja.size(); i++) {
leJSON(ja.get(i));
}
}
if (value.getClass().getSimpleName().equalsIgnoreCase("JSONObject")) {
leJSON(value);
}
}
}
}
}
主方法...
String json = "{...}";
JSONObject object = (JSONObject) JSONValue.parse(jsonString2);
leJSON(object);
英文:
I solved the problem using...
private static void leJSON(Object object) {
if (object instanceof JSONObject) {
Set < String > ks = ((JSONObject) object).keySet();
for (String key: ks) {
Object value = ((JSONObject) object).get(key);
if (value != null) {
System.out.printf("%s=%s (%s)\n", key, value, value.getClass().getSimpleName());
if (value.getClass().getSimpleName().equalsIgnoreCase("JSONArray")) {
JSONArray ja = (JSONArray) value;
for (int i = 0; i < ja.size(); i++) {
leJSON(ja.get(i));
}
}
if (value.getClass().getSimpleName().equalsIgnoreCase("JSONObject")) {
leJSON(value);
}
}
}
}
}
main method...
String json = "{...}";
JSONObject object = (JSONObject) JSONValue.parse(jsonString2);
readJSON(object);
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论