How to read a subsection from a firebase database - objective-c

I am using this code to access a Firebase Database, but I am cannot figure out how to read the sub array of strings. (the JSON structure is below)). The code I have returns the top level items, but not the list of strings. Would someone be able to assist with this issue?
Here is my function to read from the DB:
func sizes(userId: String = Auth.auth().currentUser!.uid, success: #escaping ([Sizes]) -> ()) {
let ref = Router.sizes.reference()
let query = ref.queryOrdered(byChild: "name") //userId)
query.observe(.value, with: { snapshot in
var array = [Sizes]()
for child in snapshot.children {
if let size = Mapper<Sizes>().map(JSON: (child as! DataSnapshot).value as! [String : AnyObject]) {
array.append(size)
}
}
success(array)
})
}
My Firebase JSON is as follows:
{
"-SzCat_001": {
"name": "Womans",
"sizeCategories": {
"name": "Pants",
"sizeDescriptor": [
"00",
"0",
"2",
"4",
"6",
"8",
"10",
"12",
"XL"
]
}
}
}
And this is what I get returned?
[0] = {
name = "Womans"
sizeCategories = 0 values {} }
I am trying to figure out how to read the sizeCategories list of strings as a subarray of sizes.
Here is my definition of sizes and sizeCategories:
struct Sizes: Mappable {
var name: String = ""
var sizeCategories = [SizeCategories]()
init() {
}
init?(map: Map) {
}
mutating func mapping(map: Map) {
name <- map["name"]
sizeCategories <- map["sizeCategories"]
}
}
struct SizeCategories: Mappable {
var name: String = ""
var sizeDescriptor = [String]()
init() {
}
init?(map: Map) {
}
mutating func mapping(map: Map) {
name <- map["name"]
sizeDescriptor <- map["sizeDescriptor"]
}
}
Thanks for any help!!!

You're jumping through a lot of hoops to read the data here. You could just let allMyData = snapshot.value as! [String: AnyObject] and know that each internal value is also a [String: AnyObject]. But if you really want to destructure into something more typed with this mapping technique, have a look at your sizes definition:
var sizeCategories = [SizeCategories]()
This says "sizeCategories is an array of SizeCategories type". But your data is not structured as an array, it is a dict:
"sizeCategories": {
"name": "Pants",
"sizeDescriptor": [
"00",
"0",
"2",
"4",
"6",
"8",
"10",
"12",
"XL"
]
}
You need to adjust your definition and mapping method here for this field.

Related

Kotlin - switching object detail based on group by

For example I have a class with below json format
[
{
"name": "a",
"detail": [
"1",
"2",
"3"
]
},
{
"name": "b",
"detail": [
"2",
"3",
"4"
]
}
]
how to change grouped it based on the detail?
[
{
"detail": "1",
"name": [
"a"
]
},
{
"detail": "2",
"name": [
"a",
"b"
]
},
{
"detail": "3",
"name": [
"a",
"b"
]
},
{
"detail": "4",
"name": [
"b"
]
}
]
below is my class structure
data class funName(
#field:JsonProperty("name")
val name: String = "",
#field:JsonProperty("detail")
val detail: Array<String> = arrayOf(""),
)
and my object is based on the array of funName
val data: Array<funName> = ...
i really have no idea how to do it.
val convert = data.groupBy { x -> x.detail } ??
Is this doable in kotlin/java?
Since the original data is grouped by name, you can think of the original data as a list of pairs
name detail
a 1
a 2
a 3
b 2
b 3
b 4
Mapping it to this format first would make it very easy to group by the second thing (detail) in the pair.
Since each funName corresponds to multiple pairs like this, you should use flatMap on data.
val result = data.flatMap { funName ->
funName.detail.map { funName.name to it }
}
.groupBy(keySelector = { (name, detail) -> detail }, valueTransform = { (name, detail) -> name })
// or more concisely, but less readable
// .groupBy({ it.second }) { it.first }
This will get you a Map<String, List<String>>.
If you want a List<Result>, where Result is something like
data class Result(
val detail: String = "",
val names: List<String> = listOf(),
)
You can add an additional map:
.map { (k, v) -> Result(k, v) }

flatten list with repeated element in kotlin

I have the following data class as a data transfer object.
data class Continent(
val continent: String,
val countries: List<String>
)
so the JSON response is like below.
{
"content": [
{
"continent": "Europe",
"countries": [
"France",
"Germany"
]
}
]
}
However, what I want to do is this:
{
"content": [
{
"continent": "Europe",
"country": "France"
},
{
"continent": "Europe",
"country": "Germany"
}
]
}
I suppose there must be some kotlin collection functions to apply.
Since the dto i wrote in the first code block is unable to be moderated, i have to apply collection functions on the service layer and make response dto accordingly.
Could you please let me know? Thanks in advance.
data class Country(
val continent: String,
val country: String
)
fun List<ContinentResponse>.toCountries(): List<Country> =
flatMap { r -> r.countries.map { country -> Country(r.continent, country) } }

Find the same key in other localization json-file

I want to find the same key in an other jsons localization file.
My json’s looks like this.
{
"general": {
"main": {
"title": "This is a title",
"header": "This is a header",
"sub": {
"sub_thing_1": "Something",
"sub_thing_2": "Something2"
}
}
},
"other_things": {
"title": "Other title"
"sub": {
"sub_thing_1": "Something",
"sub_thing_2": "Something2"
}
}
}
As you can see the same key can be in more places
My thinking is, to generate a collection that contains, the depth values like this
eg: this "sub_thing_2" can be found at -> ["general", "main", "sub"]
and with this information i can find the exact key in other files.
And with this code I geathering the depth information for each key, it’s just a scratch, thinkering code fragement, I'm new to Kotlin by the way
psi?.accept(object : JsonRecursiveElementVisitor() {
override fun visitObject(o: JsonObject) {
val childrens = o.children
for (child in childrens) {
val childProp = child as JsonProperty
if (child.lastChild is JsonObject) {
depth.add(childProp.name)
visitObject(child.lastChild as JsonObject)
} else {
println(depth.toString() + childProp.name + ": " + childProp.lastChild.text)
}
}
if (depth.isNotEmpty()) {
val lastItem = depth.last();
depth.remove(lastItem)
}
}
})
My question is there is other easier way to do this maybe without the PSI or I'm in the right direction?

Merging a list of maps (of list of string)

Baby Kotlin dev here :)
Considering the following structure:
[
{ "color": ["red", "blue"] },
{ "color": ["red", "green"] },
{ "shape": ["square", "circle"] },
{ "shape": ["rectangle"] }
]
I'd like to obtain the following results where keys and their respective values are merged:
[
{ "color": ["red", "blue", "green"] },
{ "shape": ["square", "circle", "rectangle"] }
]
After some research I was thinking of something like this (doesn't work/compile), but I'm missing some pieces:
val colors1 = mapOf("color" to listOf("red", "blue"))
val colors2 = mapOf("color" to listOf("red", "green"))
val shapes1 = mapOf("color" to listOf("square", "circle"))
val shapes2 = mapOf("color" to listOf("rectangle"))
var mainList = mutableListOf(colors1, colors2, shapes1, shapes2)
mainList.reduce { acc, it -> (acc.asSequence() + it.asSequence())
.distinct()
.groupBy({it.key}, {it.value})
.mapValues { (_, values) -> values.flatten().distinct() }
Any help would be greatly appreciated.
Instead of merging the maps with reduce, you can use flatMap with the map entries. So it should work this way:
mainList
.flatMap { it.entries }
.groupBy({ it.key }, { it.value })
.mapValues { (_, values) -> values.flatten().toSet() }
(runnable sample)
Also, a little bit more efficient way to flatten the values in the last line is:
.mapValues { (_, values) -> values.flatMapTo(mutableSetOf()) { it } }
This removes the overhead of the intermediate collection that was created to store the values between flatten() and distinct()/toSet(), but still ensures that the items are unique because they are added to a mutableSet().
Sample with reassigning the mainList: (link)

How to parse below Json data in Kotlin?

I need to parse this information-
[
{
"artist": "12",
"image": "23"
},
{
"video_id": "12",
"video_title": "23"
},
{
"video_id": "12",
"video_title": "23"
},
{
"video_id": "12",
"video_title": "23"
},
{
"video_id": "12",
"video_title": "23"
},
{
"video_id": "12",
"video_title": "23"
}]
As you can see the first field is different, how to parse below information differently and the first field differently in Kotlin.
I am parsing 1st part as-
response ->
for (i in 0..(response.length() -1)){
/**
FIRST SONG
**/
val song = response.get(0).toString()
val listOfSongs = response.toString()
val parser = Parser()
val stringBuilder = StringBuilder(song)
val json: JsonObject = parser.parse(stringBuilder) as JsonObject
val firstArtist = json.string("artist")
val firstImage = json.string("image")
val intent = Intent(activity,ResultPage::class.java)
intent.putExtra("firstArtist",firstArtist)
intent.putExtra("firstImage",firstImage)
startActivity(intent)
/**
FIRST SONG
**/
}
}
and am also using KLAXON library here.
For your json, this should work :
fun parseResponse(response: String) {
var artist = ""
var image = ""
val videoList = ArrayList<Video>()
val jsonArray = JSONArray(response)
(0..5).forEach { index ->
val jsonObject = jsonArray.getJSONObject(index)
if (jsonObject.has("artist") && jsonObject.has("image")) {
artist = jsonObject.getString("artist")
image = jsonObject.getString("image")
}
else if (jsonObject.has("video_id") && jsonObject.has("video_title")) {
val newVideo = Video(jsonObject.getString("video_id"), jsonObject.getString("video_title"))
videoList.add(newVideo)
}
}
}
class Video(val id: String, val title: String)
But this way is very lengthy and unnecessary. I would suggest use an Object Mapping library like GSON or Moshi.
For that, the video list in your json should ideally be something like:
[
{
"artist": "12",
"image": "23",
"videos": [
{
"video_id": "12",
"video_title": "23"
},
{
"video_id": "12",
"video_title": "23"
},
{
"video_id": "12",
"video_title": "23"
},
{
"video_id": "12",
"video_title": "23"
},
{
"video_id": "12",
"video_title": "23"
}
]
}
]
Using this Json, you can easily create a class for this object, e.g.
class Artist(val id: String, val name: String, val image: String, val videos: List<Video>)
class Video(#field:Json(name = "video_id") val id: String, #field:Json(name = "video_title") val title: String)
And parse them easily like this:
Moshi.Builder().build().adapter(Artist::class.java).fromJson(response)
and then access this information like:
val artist = Moshi.Builder().build().adapter(Artist::class.java).fromJson(response)
intent.putExtra("firstArtist",artist?.name)
intent.putExtra("firstImage",artist?.image)
You can use below code to parse given json in kotlin
private fun parseJson(jsonResponse: String){
val jsonArray = JSONArray(jsonResponse)
for (i in 0..jsonArray!!.length() - 1) {
val jsonObj = jsonArray.optJSONObject(i)
val artist =jsonObj.optString("artist")
val image =jsonObj.optString("image")
val videosArray = jsonObj.optJSONArray("videos")
for (i in 0..videosArray!!.length() - 1) {
val videoObj = jsonArray.optJSONObject(i)
val video_id =videoObj.optString("video_id")
val video_title =videoObj.optString("video_title")
}
}
}