Issue
I stored a mutableMap as a JSON, and then retrieved it. But when trying to get any of the data every key returns null. I checked on the android developer debugger and I can see that GSON retrieved all the keys as if they were strings. Is it possible to store Maps with Integers as keys in a JSON? Or do I have to store it with Strings as keys instead?
This is the declaration of the variable
val mapYears: MutableMap<Int, MutableMap<Int, MutableMap<Int, MutableMap<MutableMap<String, Int>, Int>>>>
= HashMap<Int, MutableMap<Int, MutableMap<Int, MutableMap<MutableMap<String, Int>, Int>>>>()
And this is one example of the map I'm trying to store, it's basically a map of Years > Months > Days > Time
"2022":{
"1":{
"1":{
},
"2":{
},
"3":{
},
"4":{
},
"5":{
},
"6":{
},
"7":{
},
"8":{
},
"9":{
},
"10":{
},
"11":{
},
"12":{
},
"13":{
},
"14":{
},
"15":{
},
"16":{
},
"17":{
},
"18":{
},
"19":{
},
"20":{
},
"21":{
},
"22":{
},
"23":{
},
"24":{
},
"25":{
},
"26":{
},
"27":{
},
"28":{
},
"29":{
},
"30":{
}
},
"2":{
"1":{
},
"2":{
},
"3":{
},
"4":{
},
"5":{
},
"6":{
},
"7":{
},
"8":{
},
"9":{
},
"10":{
},
"11":{
},
"12":{
},
"13":{
},
"14":{
},
"15":{
},
"16":{
},
"17":{
},
"18":{
},
"19":{
},
"20":{
},
"21":{
},
"22":{
},
"23":{
},
"24":{
},
"25":{
},
"26":{
},
"27":{
},
"28":{
},
"29":{
},
"30":{
}
},
"3":{
"1":{
},
"2":{
},
"3":{
},
"4":{
},
"5":{
},
"6":{
},
"7":{
},
"8":{
},
"9":{
},
"10":{
},
"11":{
},
"12":{
},
"13":{
},
"14":{
},
"15":{
},
"16":{
},
"17":{
},
"18":{
},
"19":{
},
"20":{
},
"21":{
},
"22":{
},
"23":{
},
"24":{
},
"25":{
},
"26":{
},
"27":{
},
"28":{
},
"29":{
},
"30":{
}
},
"4":{
"1":{
},
"2":{
},
"3":{
},
"4":{
},
"5":{
},
"6":{
},
"7":{
},
"8":{
},
"9":{
},
"10":{
},
"11":{
},
"12":{
},
"13":{
},
"14":{
},
"15":{
},
"16":{
},
"17":{
},
"18":{
},
"19":{
},
"20":{
},
"21":{
},
"22":{
},
"23":{
},
"24":{
},
"25":{
},
"26":{
},
"27":{
},
"28":{
},
"29":{
},
"30":{
}
},
"5":{
"1":{
},
"2":{
},
"3":{
},
"4":{
},
"5":{
},
"6":{
},
"7":{
},
"8":{
},
"9":{
},
"10":{
},
"11":{
},
"12":{
},
"13":{
},
"14":{
},
"15":{
},
"16":{
},
"17":{
},
"18":{
},
"19":{
},
"20":{
},
"21":{
},
"22":{
},
"23":{
},
"24":{
},
"25":{
},
"26":{
},
"27":{
},
"28":{
},
"29":{
},
"30":{
}
},
"6":{
"1":{
},
"2":{
},
"3":{
},
"4":{
},
"5":{
},
"6":{
},
"7":{
},
"8":{
},
"9":{
},
"10":{
},
"11":{
},
"12":{
},
"13":{
},
"14":{
},
"15":{
},
"16":{
},
"17":{
},
"18":{
},
"19":{
},
"20":{
},
"21":{
},
"22":{
},
"23":{
},
"24":{
},
"25":{
},
"26":{
},
"27":{
},
"28":{
},
"29":{
},
"30":{
}
},
"7":{
"1":{
},
"2":{
},
"3":{
},
"4":{
},
"5":{
},
"6":{
},
"7":{
},
"8":{
},
"9":{
},
"10":{
},
"11":{
},
"12":{
},
"13":{
},
"14":{
},
"15":{
},
"16":{
},
"17":{
},
"18":{
},
"19":{
},
"20":{
},
"21":{
},
"22":{
},
"23":{
},
"24":{
},
"25":{
},
"26":{
},
"27":{
},
"28":{
},
"29":{
},
"30":{
}
},
"8":{
"1":{
},
"2":{
},
"3":{
},
"4":{
},
"5":{
},
"6":{
},
"7":{
},
"8":{
},
"9":{
},
"10":{
},
"11":{
},
"12":{
},
"13":{
},
"14":{
},
"15":{
},
"16":{
},
"17":{
},
"18":{
},
"19":{
},
"20":{
},
"21":{
},
"22":{
},
"23":{
},
"24":{
},
"25":{
},
"26":{
},
"27":{
},
"28":{
},
"29":{
},
"30":{
}
},
"9":{
"1":{
},
"2":{
},
"3":{
},
"4":{
},
"5":{
},
"6":{
},
"7":{
},
"8":{
},
"9":{
},
"10":{
},
"11":{
},
"12":{
},
"13":{
},
"14":{
},
"15":{
},
"16":{
},
"17":{
},
"18":{
},
"19":{
},
"20":{
},
"21":{
},
"22":{
},
"23":{
},
"24":{
},
"25":{
},
"26":{
},
"27":{
},
"28":{
},
"29":{
},
"30":{
}
},
"10":{
"1":{
},
"2":{
},
"3":{
},
"4":{
},
"5":{
},
"6":{
},
"7":{
},
"8":{
},
"9":{
},
"10":{
},
"11":{
},
"12":{
},
"13":{
},
"14":{
},
"15":{
},
"16":{
},
"17":{
},
"18":{
},
"19":{
},
"20":{
},
"21":{
},
"22":{
},
"23":{
},
"24":{
},
"25":{
},
"26":{
},
"27":{
},
"28":{
},
"29":{
},
"30":{
}
},
"11":{
"1":{
},
"2":{
},
"3":{
},
"4":{
},
"5":{
},
"6":{
},
"7":{
},
"8":{
},
"9":{
},
"10":{
},
"11":{
},
"12":{
},
"13":{
},
"14":{
},
"15":{
},
"16":{
},
"17":{
},
"18":{
},
"19":{
},
"20":{
},
"21":{
},
"22":{
},
"23":{
},
"24":{
},
"25":{
},
"26":{
},
"27":{
},
"28":{
},
"29":{
},
"30":{
}
},
"12":{
"1":{
},
"2":{
},
"3":{
},
"4":{
},
"5":{
},
"6":{
},
"7":{
},
"8":{
},
"9":{
},
"10":{
},
"11":{
},
"12":{
},
"13":{
},
"14":{
},
"15":{
},
"16":{
},
"17":{
},
"18":{
},
"19":{
},
"20":{
},
"21":{
},
"22":{
},
"23":{
},
"24":{
},
"25":{
},
"26":{
},
"27":{
},
"28":{
},
"29":{
},
"30":{
}
}
}
}
This is how I store the map:
val stepsRecord: String = Gson().toJson(mapYears)
val editor = sharedPreferences.edit();
editor.putString("stepsRecord", stepsRecord);
editor.commit();
And this is how I retrieve the map:
val mapYearMonthsAny: MutableMap<*, *>? = Gson().fromJson(mapYearMonthsString, MutableMap::class.java)
return mapYearMonthsAny as MutableMap<Int, MutableMap<Int, MutableMap<Int, MutableMap<MutableMap<String, Int>, Int>>>>
Any idea what I'm doing wrong? Should I use another unserializing method? Thanks in advance for all the answers!
Solution
Found the answer!
The problem isn't on the serializing of the item but on the unserializing. You can specify the type of the object you're retrieving like this
val type: Type = object : TypeToken<MutableMap<Int, MutableMap<Int, MutableMap<Int, MutableMap<MutableMap<String, Int>, Int>>>>?>() {}.type
Then Gson changes the type accordingly, so you can use Integer keys on the map or whatever you want if you specify the type as a parameter.
val mapYearMonthsAny: MutableMap<*, *>? = Gson().fromJson(mapYearMonthsString, type)
Answered By - Layer891203
Answer Checked By - Senaida (JavaFixing Volunteer)