【问题标题】:Converting json string to map将json字符串转换为地图
【发布时间】:2018-04-13 15:05:57
【问题描述】:

我有一个 json 字符串,我的要求是转换为 map,其中 key 是 json 的字段。下面是我的 json

{  
   "A":[  
      {  
         "B":[  
            {  
               "C":[  
                  {  
                     "D1":"V1",
                     "D2":"X1",
                     "D3":Y1,
                     "D4":"Z1"
                  },
                  {  
                     "D1":"V2",
                     "D2":"X2",
                     "D3":Y2,
                     "D4":"Z2"
                  }
               ]
            }
         ]
      }
   ]
}

键应该看起来像“A->B->C->D1”和对应的值V1,V2。 地图签名应类似于Map<String,List<String>>here 发布了类似的问题,但我的问题是从 json 字段中创建密钥。如果需要更多信息,请告诉我。提前致谢。

【问题讨论】:

  • 我认为你必须建立自己的方法来做到这一点。
  • 这是一个简单的link 将json值映射到java对象
  • 谢谢@MohamedELAYADI 我有基本的知识,但我的问题是创建密钥。我想知道如果我们可以从 json 字段创建密钥,是否有一些库可以使用它。我正在寻找简化的解决方案如果可能的话。
  • 首先,您的结构是否已修复或您正在寻找通用的东西?你甚至知道停止的水平吗?
  • 结构已修复

标签: java json hashmap


【解决方案1】:

我做了一些事情来回答你的 exact 结构,我将 D3 的值也更改为字符串:

class Wraper 即整个对象

    public class Wraper {
    public Wraper() {}
    @JsonProperty("A") A[] a;
}

A

public class A {
    @JsonProperty("B") B[] b;
}

班级B

public class B {
    @JsonProperty("C") C[] c;
}

班级C

public class C {
    @JsonProperty("D1") String d1;
    @JsonProperty("D2") String d2;
    @JsonProperty("D3") String d3;
    @JsonProperty("D4") String d4;
}

最后是我测试的地方:

static final String JSON_VAL="{\"A\":[{\"B\":[{\"C\":[{\"D1\":\"V1\",\"D2\":\"X1\",\"D3\":\"Y1\",\"D4\":\"Z1\"},{\"D1\":\"V2\",\"D2\":\"X2\",\"D3\":\"Y2\",\"D4\":\"Z2\"}]}]}]}";


final ObjectMapper mapper = new ObjectMapper();
final Wraper wraper = mapper.readValue(JSON_VAL, Wraper.class);

final Map<String,List<String>> map = new HashMap<>();

Arrays.stream(wraper.a).forEach(a -> {
    Arrays.stream(a.b).forEach(b -> {
        final List<String> d1 = new ArrayList<>();
        final List<String> d2 = new ArrayList<>();
        final List<String> d3 = new ArrayList<>();
        final List<String> d4 = new ArrayList<>();
        Arrays.stream(b.c).forEach(c -> {
            d1.add(c.d1);
            d2.add(c.d2);
            d3.add(c.d3);
            d4.add(c.d4);
        });
        map.put("A->B->C->D1", d1);
        map.put("A->B->C->D2", d2);
        map.put("A->B->C->D3", d3);
        map.put("A->B->C->D4", d4);
    });
});

【讨论】:

    【解决方案2】:

    使用正确的 Java 类定义(基于 JSON)尝试使用 Jackson。

    这里有一些代码:

    public class topElement
    {
        private ElementA[] A;
    
        public ElementA[] getA()
        {
            return A;
        }
    
        public void setA(
            final ElementA[] newValue)
        {
            A = newValue;
        }
    }
    
    public class ElementA
    {
        private ElementB[] B;
    
        public ElementB[] getB()
        {
            return B;
        }
    
        public void setB(
            final ElementB[] newValue)
        {
            B = newValue;
        }
    }
    
    public class ElementB
    {
        private ElementC[] C;
    
        public ElementC[] getC()
        {
            return C;
        }
    
        public void setC(
            final ElementC[] newValue)
        {
            C = newValue;
        }
    }
    
    public class ElementC
    {
        private Map blammyMap;
    
        public Map getBlammyMap()
        {
            return blammyMap;
        }
    
        public void setBlammyMap(
            final Map newValue)
        {
            blammyMap = newValue;
        }
    }
    }
    

    【讨论】:

      猜你喜欢
      • 2017-03-22
      • 1970-01-01
      • 2021-02-18
      • 2013-11-25
      • 1970-01-01
      • 2018-06-28
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多