last day (31 days later) » 

11:46 AM
0
Q: Perfect JSON structure

Tarvo MäeseppI've been looking around and learning JSON a little bit. I thought it would be good to start learning with something easy but it seems it is not. I am trying to do JSON database. For example it has brand names and every brand has its own products with some info. I've done that like this which is ...

 
means you want all brand names seperately????
 
@EICaptainv2.0 I actually do not know. That's why I am confused. One guy in the Chat told me I have to do this and I didn't understand it and that's why I asked here.
 
can you explain what you actually want to do ... so I can help you
 
First I am trying to get the Brand names from the JSON file and put them into tableView. After, lets say CATCH is pressed I want to show "Products" those which belongs to the CATCH Brand. I am taking it slow to learn so that is the first thing I want to do. I've never done anything with networking before that's why it is so complicated to me.
 
ok ... so there are variety of brands and all brands have different child ... so you want to add brandnames in tableview and click on that you want its all products ... Am I right????
 
11:46 AM
Yes. Totally right
Hey :)
 
ok first of all one thing I just check and i.e. why you make 2 blocks that is block in block in your code???
 
It is typo. I know it is not right
 
ohhh .. ok
so this is your actual code????
self.ref = FIRDatabase.database().reference().child("Snuses").child("Brands")

self.ref.observeEventType(.Value, withBlock: { snapshot -> Void in
for brands in snapshot.children {
print(brands)
}
})
 
yes. I actually had the tableView functions also but I removed them because I think I don't need them for testing purpose I just want to print them into console
 
than print(brands) doesn't give you response???
I think it should print ... Your code is right I think
 
11:54 AM
it prints all the data I have in the JSON
 
so ... than whats the problem ... is any thing I am missing???
 
I am not able to print only Brand names
 
print(brands.key)
 
it gives me the error "abigious use of 'key'"
 
ohhh ok
wait ... I'll provide you another code
self.ref = FIRDatabase.database().reference().child("Snuses").child("Brands")
.observeSingleEventOfType(.Value, withBlock: { (snapshot) in
if snapshot.exists() {
for child in (snapshot.value?.allKeys)! {

print(child)

}
}
})
try this
 
12:00 PM
Ambiguous reference to member 'database()'
wait
it prints all brand names
wohoo
What I did wrong?
 
I think its all about snapshot.value?.allKeys)
and not useing snapshot.children...but i think snapshot.children method too gives you brand name by key .. but never use this method ... so don't know much about it
snapshot.value?.allvalues gives you all values
 
Okay. but is it right way to add those values into an array and show them in tableview?


override func viewDidLoad() {
super.viewDidLoad()


var ref = FIRDatabase.database().reference().child("Snuses").child("Brands")
.observeSingleEventOfType(.Value, withBlock: { (snapshot) in
if snapshot.exists() {
for child in (snapshot.value?.allKeys)! {
self.snusBrandsArray.insert(child as! String, atIndex: 0)

print(child)

}
}
})

print(snusBrandsArray)

}

override func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
 
is it working?????
 
nope :D
 
ohhh :)
you got all keys in array???
 
12:11 PM
I think so since it printed all those out and then I inserted it inside the same function like that: self.snusBrandsArray.insert(child as! String, atIndex: 0)
and then loaded those strings label1.text! = snusBrandsArray[indexPath.row]
 
no .. no
just use snusBrandsArray = child
 
It gives signal sigabrt
Could not cast value of type 'NSTaggedPointerString' (0x10971dae8) to 'NSArray' (0x10971db88).
 
how did you declare snusBrandsArray
 
[String]() Or should I make Struct and declare it as struct?
 
declare it as var snusBrandsArray = [String]()
 
12:20 PM
I did
 
ohhh wait
self.ref = FIRDatabase.database().reference().child("Snuses").child("Brands")

self.ref.observeEventType(.Value, withBlock: { snapshot -> Void in
if snapshot.exists() {
snusBrandsArray = (snapshot.value?.allKeys)!

}
})
 
'AnyObject' is not a subtype of 'NSDictionary' You can check the project here if you have time: github.com/MaeseppTarvo/Firebase
 
ok ... let me check it and update you
but there is no code in your project @TarvoMäesepp
 
chekc now
 
12:47 PM
@TarvoMäesepp your structure is wrong
 
the problem is here
"CATCH": [
{
"Products": "CATCH EUCALYPTUS WHITE LARGE",
"nicotine": "8.0"
},
just use dictionary instead array
ohhh ... sorry ... you can use it like this too
 
Yep. Even validators validated it
 
json is validate ... I said structure might be wrong ... but no issue here
@TarvoMäesepp ok done
its working now
just connect outlet of table and its working for you :)
 
1:07 PM
yep, its loading now. Thank you so much. I couldn't done it by myself. Can I now use the

func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {

}

To show the brand products?
 
yes but you need to get allValues first in one array .. or use array of dictionary
 
Same way as we got the Brands?
 
yes ... but use allvalues instead allKeys ...
If I have a time tomorrow .. I'll make it maybe
for now can I post it as an answer???
 
Yes, absolutely :)
Thank you
 
you're welcome brother ... anytime :)
If possible I make it with values too ... but tomorrow may be :)
 
1:16 PM
Okay. let me know :)
I will look and try to do things too. The JSON is so complicated
 
ok ... bye :)
 
Bye ;)
 

  last day (31 days later) »