1

Hy guys I have this HashMap:

HashMap<User, HashMap<ArrayList<User>, Yield>> userYield = new HashMap<>();

User and Yelda are objects and, after I fill the HashMap, I've to put the HashMap in a JSON, how I could be?

Edit: with JSONObject this is the result :

{
" id:4575, nome:Bill, cognome:Man": {
    "[ id:3743, nome:John, cognome:Redo,  id:3243, nome:Gian, cognome:Vand,  id:34243, nome:Mike, cognome:Mill]": {
        "incassoAzienda": 80,
        "incasso": 125,
        "idRendimento": 1,
        "incassoAgenzia": 45
    }
}

} What I want is something like this, if it is possible:

{
" id:4575, nome:Bill, cognome:Man": {
    "{ id:3743, nome:"John", cognome:"Redo",  id:"3243", nome:"Gian", cognome:"Vand",  id:"34243", nome:"Mike", cognome:"Mill"}": {
        "incassoAzienda": 80,
        "incasso": 125,
        "idRendimento": 1,
        "incassoAgenzia": 45
    }
}

}

7
  • There are several ways to do this. What have you tried so far? Commented Feb 22, 2022 at 7:56
  • 2
    Note that HashMap<User, HashMap<ArrayList<User>, Yield>> would mean json like { "gianluca23" : { ["gianluca23"] : {"someYield"}}}, i.e. you'd want to use a list as the key. I might be missing something but I'm pretty sure that's not valid json. So what should the resulting json look like or at least what do you want it to express (and what would the nested maps express)? (And what did you try so far?) Commented Feb 22, 2022 at 8:03
  • @Thomas I edit the post with what I tried and what I want if it is possible Commented Feb 22, 2022 at 8:17
  • I also tried with new Gson().toJson but the JSON is not valid Commented Feb 22, 2022 at 8:18
  • 1
    You can't use objects as keys in json. What you've done so far would be converting the key to a json string and using that string as a key in json. While this can be done this can lead to hard-to-use structures since json doesn't have any ordering requirement so the string might be different even if the data didn't change (attributes can be in different order). Also, why exactly is your nested map's key a list? What exactly does that mean? Commented Feb 22, 2022 at 8:48

1 Answer 1

0

try GSON library from Google

add this to gradle

implementation 'com.google.code.gson:gson:2.8.6'

Add the following methods to the User class

import com.google.gson.Gson;

public String toJson(){
    return new Gson().toJson(this);
}
public static User initFromJson(String json){
    return new Gson().fromJson(json, User.class);
}
public static List<User> initFromJsonArray(String json){
    List<User> lst = new ArrayList<>();
    lst.addAll(Arrays.asList(new Gson().fromJson(json, User[].class)));
    return lst;
}
public static String listToJson(List<User> lst){
    return new Gson().toJson(lst.toArray());
}
Sign up to request clarification or add additional context in comments.

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.