0

I have hashmap string object below. How to get value name from object SubSource.value index 0? I just found function for get first object, for example I just get value from test with hashMapValue.get("test"). How to get value object inside the object? should I convert to json and I get the value? Thanks.

{
  "test" : {
    "type" : "",
    "value" : ""
  },
  "Attachment" : {
    "type" : "",
    "value" : ""
  },
  "SubSource" : {
    "type" : "string",
    "value" : [ {
      "address" : "[email protected]",
      "name" : "bobby"
    }, {
      "address" : "[email protected]",
      "name" : "2sadasd"
    }, {
      "address" : "[email protected]",
      "name" : "ggfgf"
    } ]
  }
}

My code:

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", "[email protected]");

// create a fresh map
Map<String,Object> subSourceMap2 = new HashMap<>();
subSourceMap2.put("name", "2sadasd");
subSourceMap2.put("address", "[email protected]");      

// create a fresh map
Map<String,Object> subSourceMap3 = new HashMap<>();
subSourceMap3.put("name", "ggfgf");
subSourceMap3.put("address", "[email protected]");    

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
  • 3
    "I have hashmap string object below" -- Actually what you have is a bit of JSON. Please edit your post and show us the Java code that parses this and loads it into a HashMap. Commented Feb 13, 2018 at 7:30
  • okay. I have already changed. Commented Feb 13, 2018 at 7:37
  • What is the context? Attachment will be every time just one, or can be more than one? Are you expecting unique addresses? Commented Feb 13, 2018 at 8:28
  • If you are having trouble with reading json, than take a look eg. there mkyong.com/java/json-simple-example-read-and-write-json Commented Feb 13, 2018 at 9:13

4 Answers 4

1

Not sure what you are asking, but...

A) Cast the object you are getting from the map to the Object type you are trying to grab the values out of

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

B) Add type parameters to your map

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

C) If you are wondering how to get those maps out of a list

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();
Sign up to request clarification or add additional context in comments.

1 Comment

how to get the value from variable departmentPHSSuportEmail?
1

This is a JSON string. Find a proper JSON deserializer library and include that in your project instead of coding all this HashMap stuff.. ;)

Ie this jackson-2-convert-object-to-from-json

Comments

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

2 Comments

how to get the value from variable departmentPHSSuportEmail?
the variable is specified in the first line of code in the answer. you can copy paste the code and it will work. what is unclear??
0

There are many ways how to solve this, one of solutions can be like this:

  • make container for member data (address + name), if the addresses should be unique, than can be used as a key in map
  • make container for attachment
  • make container for the whole data, because of data integrity

Please, notice, I have used hashmap for members, but arraylist for attachments, if you are expecting also unique values at attachments, than you can use also hashmap with key name, then you can use it for finding

So..

Member container

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

Container for attachments

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

Than container for complete data

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

Filling example

        Map<String, Member> membersMap= new HashMap<String, Member>();
        Member newMember = new Member("fooo Name", "[email protected]");
        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", "[email protected]"));
        data.addAttachment(new Attachment("another attachmetn", "anotherAtt value"));

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.