读取哈希映射嵌套属性动态地。

huangapple go评论68阅读模式
英文:

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:

&quot;total&quot;:&quot;2&quot;,
&quot;offset&quot;:&quot;1&quot;,
&quot;limit&quot;:&quot;2&quot;,
&quot;results&quot;:[{    
    &quot;code&quot;:1,
    &quot;title&quot;:&quot;RESTAURANTE SADOCHE&quot;,
    &quot;contact&quot;:{
        &quot;code&quot;:10,
        &quot;name&quot;:&quot;HENRIQUE BARBALHO&quot;,
        &quot;company&quot;:{
            &quot;code&quot;:100,
            &quot;name&quot;:&quot;RESTAURANTE SADOCHE LTDA-ME&quot;
        }
    }
},
{
    &quot;code&quot;:2,
    &quot;title&quot;:&quot;ARNALDO GRILL&quot;,
    &quot;contact&quot;:{
        &quot;code&quot;:20,
        &quot;name&quot;:&quot;F&#193;TIMA COSTA&quot;,
        &quot;company&quot;:{
            &quot;code&quot;:200,
            &quot;name&quot;:&quot;COSTA NATAL RESTAURANTE EIRELI&quot;
        }
    }
}]

I turned this JSON into a Java HashMap using the Gson library.

Map&lt;String, Object&gt; retMap = new Gson().fromJson(jsonUpString, new TypeToken&lt;HashMap&lt;String, Object&gt;&gt;(){}.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 = &quot;name&quot;;
String nesting = &quot;results;contact;company&quot;;
String[] levels = nesting.split(&quot;;&quot;);

Map map = new HashMap();
map = retMap;

for (int i = 0; i &lt; 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 &lt; String &gt; ks = ((JSONObject) object).keySet();
        for (String key: ks) {
            Object value = ((JSONObject) object).get(key);
            if (value != null) {
                System.out.printf(&quot;%s=%s (%s)\n&quot;, key, value, value.getClass().getSimpleName());
                if (value.getClass().getSimpleName().equalsIgnoreCase(&quot;JSONArray&quot;)) {
                    JSONArray ja = (JSONArray) value;
                    for (int i = 0; i &lt; ja.size(); i++) {
                        leJSON(ja.get(i));
                    }
                }
                if (value.getClass().getSimpleName().equalsIgnoreCase(&quot;JSONObject&quot;)) {
                    leJSON(value);
                }
            }
        }
    }
}

main method...

String json = &quot;{...}&quot;;
JSONObject object = (JSONObject) JSONValue.parse(jsonString2);        
readJSON(object);

huangapple
  • 本文由 发表于 2020年8月7日 03:50:50
  • 转载请务必保留本文链接:https://go.coder-hub.com/63290802.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定