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

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

Read hashmap nested properties dynamically

问题

我有以下JSON:

  1. "total":"2",
  2. "offset":"1",
  3. "limit":"2",
  4. "results":[{
  5. "code":1,
  6. "title":"RESTAURANTE SADOCHE",
  7. "contact":{
  8. "code":10,
  9. "name":"HENRIQUE BARBALHO",
  10. "company":{
  11. "code":100,
  12. "name":"RESTAURANTE SADOCHE LTDA-ME"
  13. }
  14. }
  15. },
  16. {
  17. "code":2,
  18. "title":"ARNALDO GRILL",
  19. "contact":{
  20. "code":20,
  21. "name":"FÁTIMA COSTA",
  22. "company":{
  23. "code":200,
  24. "name":"COSTA NATAL RESTAURANTE EIRELI"
  25. }
  26. }
  27. }]

我使用Gson库将此JSON转换为Java HashMap:

  1. Map<String, Object> retMap = new Gson().fromJson(jsonUpString, new TypeToken<HashMap<String, Object>>(){}.getType());

我需要动态读取此创建的哈希映射的某些属性。例如:标题,联系人的姓名和公司的名称。

有时,这些属性(标题,联系人的姓名和公司的名称)可能在列表中。

以下是我的代码:

  1. String propertyName = "name";
  2. String nesting = "results;contact;company";
  3. String[] levels = nesting.split(";");
  4. Map map = new HashMap();
  5. map = retMap;
  6. for (int i = 0; i < niveis.length; i++) {
  7. map = (Map)map.get(levels[i]);
  8. System.out.println(map);
  9. if (i == levels.length - 1) {
  10. System.out.println(map.get(propertyName));
  11. }
  12. }

但是,如果属性(results,contact或company)返回多个对象,JSON会将它们作为列表返回,我无法获取所需的信息。

英文:

I have the JSON below:

  1. &quot;total&quot;:&quot;2&quot;,
  2. &quot;offset&quot;:&quot;1&quot;,
  3. &quot;limit&quot;:&quot;2&quot;,
  4. &quot;results&quot;:[{
  5. &quot;code&quot;:1,
  6. &quot;title&quot;:&quot;RESTAURANTE SADOCHE&quot;,
  7. &quot;contact&quot;:{
  8. &quot;code&quot;:10,
  9. &quot;name&quot;:&quot;HENRIQUE BARBALHO&quot;,
  10. &quot;company&quot;:{
  11. &quot;code&quot;:100,
  12. &quot;name&quot;:&quot;RESTAURANTE SADOCHE LTDA-ME&quot;
  13. }
  14. }
  15. },
  16. {
  17. &quot;code&quot;:2,
  18. &quot;title&quot;:&quot;ARNALDO GRILL&quot;,
  19. &quot;contact&quot;:{
  20. &quot;code&quot;:20,
  21. &quot;name&quot;:&quot;F&#193;TIMA COSTA&quot;,
  22. &quot;company&quot;:{
  23. &quot;code&quot;:200,
  24. &quot;name&quot;:&quot;COSTA NATAL RESTAURANTE EIRELI&quot;
  25. }
  26. }
  27. }]

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

  1. 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:

  1. String propertyName = &quot;name&quot;;
  2. String nesting = &quot;results;contact;company&quot;;
  3. String[] levels = nesting.split(&quot;;&quot;);
  4. Map map = new HashMap();
  5. map = retMap;
  6. for (int i = 0; i &lt; niveis.length; i++) {
  7. map = (Map)map.get(levels[i]);
  8. System.out.println(map);
  9. if (i == levels.length - 1) {
  10. System.out.println(map.get(propertyName));
  11. }
  12. }

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

我使用以下方式解决了这个问题...

  1. private static void leJSON(Object object) {
  2. if (object instanceof JSONObject) {
  3. Set<String> ks = ((JSONObject) object).keySet();
  4. for (String key : ks) {
  5. Object value = ((JSONObject) object).get(key);
  6. if (value != null) {
  7. System.out.printf("%s=%s (%s)\n", key, value, value.getClass().getSimpleName());
  8. if (value.getClass().getSimpleName().equalsIgnoreCase("JSONArray")) {
  9. JSONArray ja = (JSONArray) value;
  10. for (int i = 0; i < ja.size(); i++) {
  11. leJSON(ja.get(i));
  12. }
  13. }
  14. if (value.getClass().getSimpleName().equalsIgnoreCase("JSONObject")) {
  15. leJSON(value);
  16. }
  17. }
  18. }
  19. }
  20. }
  21. 主方法...
  22. String json = "{...}";
  23. JSONObject object = (JSONObject) JSONValue.parse(jsonString2);
  24. leJSON(object);
英文:

I solved the problem using...

  1. private static void leJSON(Object object) {
  2. if (object instanceof JSONObject) {
  3. Set &lt; String &gt; ks = ((JSONObject) object).keySet();
  4. for (String key: ks) {
  5. Object value = ((JSONObject) object).get(key);
  6. if (value != null) {
  7. System.out.printf(&quot;%s=%s (%s)\n&quot;, key, value, value.getClass().getSimpleName());
  8. if (value.getClass().getSimpleName().equalsIgnoreCase(&quot;JSONArray&quot;)) {
  9. JSONArray ja = (JSONArray) value;
  10. for (int i = 0; i &lt; ja.size(); i++) {
  11. leJSON(ja.get(i));
  12. }
  13. }
  14. if (value.getClass().getSimpleName().equalsIgnoreCase(&quot;JSONObject&quot;)) {
  15. leJSON(value);
  16. }
  17. }
  18. }
  19. }
  20. }

main method...

  1. String json = &quot;{...}&quot;;
  2. JSONObject object = (JSONObject) JSONValue.parse(jsonString2);
  3. 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:

确定