Issue on JSON nested Array objects into TableVIew - ios

I've tried many ways but i'm unable get the data from Something Array and show it in UITableView based on indexPath.
something is like below response
{
"results": [
{
"Something": [
[
{
"airV": "LX",
"class": "Y"
}
]
],
},
{
"Something": [
[
{
"airV": "FZ",
"class": "Y"
}
],
[
{
"airV": "FZ",
"class": "Y"
}
]
],
},
],
}
Note : something array [] ->>> contains many arrays in it.Looping the something array i want to get "airV": "LX","class": "Y"
airv,class into TableView

You can store the NSArray of (something) and iterate over that array based on NSIndexPath.
NSArray * arrayofSomething /* Your Array */
Inside your cellForRowAtIndexPath you can get the objects from arrayofSomething based on index.
Obj *obj = [arrayofSomething objectAtIndex:indexPath.row];

Related

Filter an array in iOS shortcuts - how?

I have an array like this:
"vehicleStatus": [
{
"key": "TU_STATUS_PRIMARY_VOLT",
"value": "3.6"
},
{
"key": "TU_STATUS_PRIMARY_CHARGE_PERCENT",
"value": "100"
},
{
"key": "TU_STATUS_GSM_MODEM",
"value": "FUNCTIONING"
},
{
"key": "DOOR_IS_ALL_DOORS_LOCKED",
"value": "TRUE"
}
]
I want to retrieve the value of DOOR_IS_ALL_DOORS_LOCKED by filtering the array and without the "Repeat with each item" action.
I already filter arrays in other tools, JavaScript, Power Automate etc.
Is this possible in iOS Shortcuts?
I tried looping through all 97 items in this array and using IF condition to set a variable if the condition is met and then using that variable later, but it's long winded and takes too long for what I need.
1). For loop
First Solution of your problem, is already you have done.
Maybe you have done this by using for loop manually.
so second Option is
2). System Iterative
if let arrEle = vehicleStatus.first { $0.key == "DOOR_IS_ALL_DOORS_LOCKED"} {
print(arrEle.value)
}
or
if let arrEle = vehicleStatus.first { ($0 as? NsDict)?["key"] == "DOOR_IS_ALL_DOORS_LOCKED"} {
print((arrEle as? NsDict)?["value"] as? String)
}
or
if let arrEle = vehicleStatus.first { ($0 as? NsDict)?["key"] == "DOOR_IS_ALL_DOORS_LOCKED"},
let val = (arrEle as? NsDict)?["value"] as? String {
print(val)
}
you can change data management by
3). Dictionary
If 'vehicleStatus' has all data like you provide, then all data must be manage by dictionary.
let vehicleStatus = [
[
"key" : "TU_STATUS_PRIMARY_VOLT",
"value": "3.6"
],
[
"key" : "TU_STATUS_PRIMARY_CHARGE_PERCENT",
"value": "100"
],
[
"key" : "TU_STATUS_GSM_MODEM",
"value" : "FUNCTIONING"
],
[
"key" : "DOOR_IS_ALL_DOORS_LOCKED",
"value": "TRUE"
]
]
print(vehicleStatus)
let dict = Dictionary(uniqueKeysWithValues: vehicleStatus.map{ ($0["key"]!, $0["value"]! ) })
print(dict)

How to read multidimensional JSON data in google sheet appscript?

How can I read this type of json data like I want profession of ID:12311 in google spreadsheet?
{
"user": [
{
"id": 12311,
"name": "Deffy doof",
"profession": "Photographer"
},
{
"id": 18341,
"name": "John Smith",
"profession": "Developer"
}
]
}
Solution:
Since this is an array of objects you can reference elements using array notation, e.g., to pull "Photographer":
data.user[0].profession
Sample:
If you only know the unique ID and not the index, you can filter the array first, then get the element.
var array = data.user.filter(function b(e) { return e.id == 12311 });
console.log(array[0].profession);
References:
filter()

Configuring collectionView:cellForRowAt based on an array of strings

I've got this JSON data (not verbatim) that I get from the backend. It contains the actual data and an array of strings describing the sequence of cells to be shown:
{
"data": [
{
"name": "text",
"data": {
"text": "some text"
}
},
{
"name": "pic",
"data": {
"url": "https://somepic.jpg",
"text": "picture"
}
},
{
"name": "switcher",
"data": {
"id": 1,
"options": [
{
"id": 0,
"text": "option 1"
},
{
"id": 1,
"text": "option 2"
},
{
"id": 2,
"text": "option 3"
}
]
}
}
],
"view": [
"text",
"pic",
"switcher",
"text"
]
}
The problem is that I can't get my head around how to configure cellForRowAt: and get the right order of cells in one section. (i.e. text, pic, selector, text).
I tried a couple of things:
Looping through "view" array and switching on each individual view string to dequeue a specific cell but that doesn't seem to work since returning a cell from a switch case gives a "unexpected non-void return value in void function" error.
I was also thinking about turning a "view" array into a dictionary and then, based on keys in it, dequeue a specific cell but then again, a dictionary should have unique keys meaning that I will not have 2 "text" entries, one of them will be lost.
So, the questions is: how can I dequeue specific cells based on the array of strings? It's also important to understand that it should be done in one section. I'm feeling that it's somehow not that difficult to implement but I'm kinda lost right now. Thanks!
you need to transform your view list and data array into an array of cell contents that you can use inside the TableViewDelegate and TableViewSource method :
var cellsContents : [Int] = []
for aView in view {
var found = false
var index = 0
for aData in data {
if !found {
if let name = aData["name"] as? String {
if aView == name {
found = true
cellsContents.append(index)
continue
}
}
index = index + 1
}
}
}
Then :
number of rows : cellsContents.count
type and contents for a row : data[cellsContents[indexPath.row]]["name"] and data[cellsContents[indexPath.row]]["data"]

SwiftyJson: Looping an array inside an array

I am trying to loop through an array inside an array. The first loop was easy but I'm having trouble looping through the second array inside it. Any suggestions would be welcome!
{
"feeds": [
{
"id": 4,
"username": "andre gomes",
"feeds": [
{
"message": "I am user 4",
"like_count": 0,
"comment_count": 0
}
]
},
{
"id": 5,
"username": "renato sanchez",
"feeds": [
{
"message": "I am user 5",
"like_count": 0,
"comment_count": 0
},
{
"message": "I am user 5-2",
"like_count": 0,
"comment_count": 0
}
]
}
]
}
As you see I'm having trouble to reach through to the message field etc
Here's my code on swiftyjson
let json = JSON(data: data!)
for item in json["feeds"].arrayValue {
print(item["id"].stringValue)
print(item["username"].stringValue)
print(item["feeds"][0]["message"])
print(item["feeds"][0]["like_count"])
print(item["feeds"][0]["comment_count"])
}
The output I get is
4
andre gomes
I am user 4
0
0
5
renato sanchez
I am user 5
0
0
As you see I'm unable to get the message"I am user 5-2" and corresponding like_count and comment_count
You already demonstrated how to loop through a JSON array so you only need to do it again with the inner feeds:
let json = JSON(data: data!)
for item in json["feeds"].arrayValue {
print(item["id"].stringValue)
print(item["username"].stringValue)
for innerItem in item["feeds"].arrayValue {
print(innerItem["message"])
print(innerItem["like_count"])
print(innerItem["comment_count"])
}
}
If you only want the first item in the inner feeds array, replace the inner for lop with this:
print(item["feeds"].arrayValue[0]["message"])
print(item["feeds"].arrayValue[0]["like_count"])
print(item["feeds"].arrayValue[0]["comment_count"])

json parsing in swift

Here is my Json
{
"id": "63",
"name": "Magnet",
"price": "₹1250",
"description": "",
"image": [
"catalog/IMG-20150119-WA0012_azw1e3ge.jpg",
"catalog/IMG-20150119-WA0029_6mr3ndda.jpg",
"catalog/IMG-20150119-WA0028_ooc2ea52.jpg",
"catalog/IMG-20150119-WA0026_4wjz5882.jpg",
"catalog/IMG-20150119-WA0024_e38xvczi.jpg",
"catalog/IMG-20150119-WA0020_vyzhfkvf.jpg",
"catalog/IMG-20150119-WA0018_u686bmde.jpg",
"catalog/IMG-20150119-WA0016_c8ffp19i.jpg"
],
"thumb_image": [
"cache/catalog/IMG-20150119-WA0012_azw1e3ge-300x412.jpg",
"cache/catalog/IMG-20150119-WA0029_6mr3ndda-300x412.jpg",
"cache/catalog/IMG-20150119-WA0028_ooc2ea52-300x412.jpg",
"cache/catalog/IMG-20150119-WA0026_4wjz5882-300x412.jpg",
"cache/catalog/IMG-20150119-WA0024_e38xvczi-300x412.jpg",
"cache/catalog/IMG-20150119-WA0020_vyzhfkvf-300x412.jpg",
"cache/catalog/IMG-20150119-WA0018_u686bmde-300x412.jpg",
"cache/catalog/IMG-20150119-WA0016_c8ffp19i-300x412.jpg"
],
"specifications": [
{
"Fabrics": [
"Pure chiffon straight cut suits 48" length"
]
},
{
"MOQ": [
"Minimum 10"
]
}
]
}
In above json string the "specification" arraylist has dynamic number of key and each key has dynamic number of values
So how can parse this? Please help if anyone knows this...
Thanks in advance
There are multiple ways to do parsing. In your case specifications should be an array, so you'll be able to loop on each items.
You might want to :
Create your own JSON parse class / methods ;
Use an existing library to parse JSON.
For the second option, you can give a look at the following :
https://github.com/Wolg/awesome-swift#jsonxml-manipulation
var yourJson = data as? NSDictionary
if let id = yourJson.valueForKey("id") as String
{
//save your id from json
}
if let name = yourJson.valueForKey("name") as String
{
//save your name
}
...
if let images = yourJson.valueForKey("image") as NSArray
{
for im in images
{
//save image
}
//the same for all othe images
}
... And so on...
You should also watch some tutorials, to understand the basics of JSON parsing..
https://www.youtube.com/watch?v=MtcscjMxxq4

Resources