如何从哈希映射中获取对象内的值对象

时间:2018-02-13 07:28:38

标签: java hashmap

我下面有hashmap字符串对象。如何从对象SubSource.value索引0获取值名称?我刚刚找到了获取第一个对象的函数,例如我只是通过hashMapValue.get(" test")从test中获取值。如何获取对象内的值对象?我应该转换为json,我得到的价值?感谢。

{
  "test" : {
    "type" : "",
    "value" : ""
  },
  "Attachment" : {
    "type" : "",
    "value" : ""
  },
  "SubSource" : {
    "type" : "string",
    "value" : [ {
      "address" : "xxx.xxxx@xxx.co.id",
      "name" : "bobby"
    }, {
      "address" : "xxx.xxxx@xxx.co.id",
      "name" : "2sadasd"
    }, {
      "address" : "xxx.xxxx@xxx.co.id",
      "name" : "ggfgf"
    } ]
  }
}

我的代码:

Map<String, Object> departmentPHSSuportEmail = new HashMap<String, Object>();
Map<String, Object> subSourceMap = null;

List<Map<String , Object>> myMap  = new ArrayList<Map<String,Object>>();

Map<String, Object> attachment = new HashMap<String, Object>();
attachment.put("type", "");
attachment.put("value", "");
departmentPHSSuportEmail.put("Attachment", attachment);

Map<String, Object> subSource = new HashMap<String, Object>();
subSource.put("type", "string");
subSource.put("value", myMap);
departmentPHSSuportEmail.put("SubSource", subSource);

// create a fresh map
Map<String,Object> subSourceMap1 = new HashMap<>();
subSourceMap1.put("name", "bobby");
subSourceMap1.put("address", "xxx.xxxx@xxx.co.id");

// create a fresh map
Map<String,Object> subSourceMap2 = new HashMap<>();
subSourceMap2.put("name", "2sadasd");
subSourceMap2.put("address", "xxx.xxxx@xxx.co.id");      

// create a fresh map
Map<String,Object> subSourceMap3 = new HashMap<>();
subSourceMap3.put("name", "ggfgf");
subSourceMap3.put("address", "xxx.xxxx@xxx.co.id");    

myMap.add(subSourceMap1);
myMap.add(subSourceMap2);
myMap.add(subSourceMap3);

Map<String, Object> attachments = new HashMap<String, Object>();
attachments.put("type", "");
attachments.put("value", "dasda");
departmentPHSSuportEmail.put("test", attachments);

4 个答案:

答案 0 :(得分:1)

不确定你在问什么,但是......

A)将你从地图获得的对象转换为你试图从中获取值的对象类型

String name = (String) subSourceMap1.get("name");

B)向地图添加类型参数

Map<String, String> subSourceMap1 = new HashMap<String, String>();
String name = subSourceMap1.get("name");
String address = subSourceMap1.get("address");

C)如果您想知道如何从列表中获取这些地图

Map<String, YourObject> subSourceMap1 = myMap.get(0); //This is index 0's of your map subsource
//You can grab index's from 'myMap' that are less than myMap.size();

答案 1 :(得分:1)

这是一个JSON字符串。找到一个合适的JSON反序列化库并将其包含在您的项目中,而不是编写所有这些HashMap的东西..;)

是的,就是这个 jackson-2-convert-object-to-from-json

答案 2 :(得分:0)

    Map<String, Object> subSource2 = (Map<String, Object>)departmentPHSSuportEmail.get("SubSource");
    List<Map<String , Object>> myMap2  = (List<Map<String , Object>>)subSource2.get("value");
    Map<String,Object> subSourceMap3 = myMap2.get(0);
    String value = (String)subSourceMap3.get("value");

答案 3 :(得分:0)

有很多方法可以解决这个问题,其中一个解决方案可以是这样的:

  • 为成员数据(地址+名称)制作容器,如果是地址 应该是唯一的,可以用作地图中的关键
  • 制作附件容器
  • 因数据完整性而为整个数据制作容器

请注意,我已经为成员使用了hashmap,但是对于附件使用了arraylist,如果你期望附件中也有唯一值,那么你也可以使用带有键名的hashmap,那么你可以用它来查找

所以..

会员容器

public class Member {
    private String name;
    private String address;


    public Member(String name, String address) {
        super();
        this.name = name;
        this.address = address;
    }


    public String getName() {
        return name;
    }


    public void setName(String name) {
        this.name = name;
    }


    public String getAddress() {
        return address;
    }


    public void setAddress(String address) {
        this.address = address;
    }

附件的容器

public class Attachment {
    String name;
    String value;


    public Attachment(String name, String value) {
        super();
        this.name = name;
        this.value = value;
    }


    public String getName() {
        return name;
    }


    public void setName(String name) {
        this.name = name;
    }


    public String getValue() {
        return value;
    }


    public void setValue(String value) {
        this.value = value;
    }
}

比完整数据的容器

public class DataContainer {
    Map<String, Member> membersMap;
    ArrayList<Attachment> attachments;

    public DataContainer() {
        this.membersMap = new HashMap<String, Member>();
        this.attachments = new ArrayList<Attachment>();
    }

    public DataContainer(Map<String, Member> membersMap, ArrayList<Attachment> attachments) {
        super();
        this.membersMap = membersMap;
        this.attachments = attachments;
    }

    public Map<String, Member> getMembersMap() {
        return membersMap;
    }

    public void setMembersMap(Map<String, Member> membersMap) {
        this.membersMap = membersMap;
    }

    public ArrayList<Attachment> getAttachments() {
        return attachments;
    }

    public void setAttachments(ArrayList<Attachment> attachments) {
        this.attachments = attachments;
    }

    public void addAttachment(Attachment newItem) {
        this.attachments.add(newItem);
    }

    public void addMember(Member newMember) {
        this.membersMap.put(newMember.getAddress(), newMember);
    }

    public Member getMemberByAddress(String address) {
        return this.membersMap.get(address);
    }
}

填写示例

        Map<String, Member> membersMap= new HashMap<String, Member>();
        Member newMember = new Member("fooo Name", "fooo@whatever.foo");
        membersMap.put(newMember.getAddress(), newMember);

        Attachment newAtatchment = new Attachment("fooattach", "something");
        ArrayList<Attachment> attachments = new ArrayList<>();
        attachments.add(newAtatchment);


        DataContainer data = new DataContainer(membersMap, attachments);

        data.addMember(new Member("anotherMember", "fooo@foooo.foo"));
        data.addAttachment(new Attachment("another attachmetn", "anotherAtt value"));