简体   繁体   English

将json字符串转换为map

[英]Converting json string to map

I have a json string and my requirement is to covert into map, where key will be field of the json. 我有一个json字符串,我的要求是隐秘进入map,其中key将是json的字段。 below is my json 以下是我的json

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

Key should look like "A->B->C->D1" and corresponding value V1,V2. 键应类似于“ A-> B-> C-> D1”和相应的值V1,V2。 Map signature should look like Map<String,List<String>> . 地图签名应类似于Map<String,List<String>> Similar kind of question posted here but my problem is to create key out of json field.Let me know if more information is required. 这里发布类似的问题但我的问题是在json字段之外创建密钥。让我知道是否需要更多信息。 Thanks in advance. 提前致谢。

I did something that answers the exact structure of yours where i changed the value of D3 to be also string: 我做了一些可以回答您的确切结构的事情,在此我将D3的值也更改为字符串:

class Wraper that is the whole object 整个对象的Wraper

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

class A A

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

Class B B

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

Class C C

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

And finally where I tested: 最后是我测试的地方:

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);
    });
});

Try Jackson using the correct Java class definitions (based on the JSON). 使用正确的Java类定义(基于JSON)尝试Jackson。

Here is some code: 这是一些代码:

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;
    }
}
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM