I have a JSON response (over 500lines) , that has several arrays and brackets inside. Basically an overly nested response.
I am using Alamofire for fetching and i get a JSONData as response. But I don't know how to take values from the response and print it on a custom cell .
I am using Alamofire 5 to get the JSON response.
Please excuse if this sounds too newbieish , I am new to iOS developing.
{
"availabilityResultList": [
{
"availabilityRouteList": [
{
"availabilityByDateList": [
{
"originDestinationOptionList": [
{
"fareComponentGroupList": [
{
"boundList": [
{
"availFlightSegmentList": [
{
"addOnSegment": false,
"flightSegment": {
"journeyDuration": "PT1H15M",
"distance": 1,
"departureAirport": {
"locationName": "LOC",
"cityInfo": {
"country": {
"locationName": "LOC",
"currency": {
"code": "USD"
},
"locationCode": "LOC",
"locationNameLanguage": "EN"
},
"city": {
"locationName": "LOC",
"locationCode": "LOC",
"locationNameLanguage": "EN"
}
},
"codeContext": "ASD",
"language": "EN",
"locationCode": "LOC"
},
"equipment": {
"airEquipType": "ABC",
"changeofGauge": false
},
"remark": "NO",
"ticketType": "PP",
"onTimeRate": 0,
"flightNotes": [
{
"deiCode": 0
}
],
"flightSegmentID": "",
"flownMileageQty": 0,
"flightNumber": "106",
"groundDuration": "",
"trafficRestriction": {
"code": "",
"explanation": {}
},
"stopQuantity": 0,
"codeshare": false,
"secureFlightDataRequired": false,
"departureDateTime": {
"month": 5,
"hour": 6,
"year": 2019,
"timezone": 270,
"day": 6,
"minute": 45,
"second": 0
},
"ondControlled": false,
"arrivalDateTime": {
"month": 5,
"hour": 8,
"year": 2019,
"timezone": 270,
"day": 6,
"minute": 0,
"second": 0
},
"airline": {
"code": "RQ"
},
"arrivalAirport": {
"locationName": "LOC",
"cityInfo": {
"country": {
"locationName": "AA",
"currency": {
"code": "USD"
},
"locationCode": "AA",
"locationNameLanguage": "EN"
},
"city": {
"locationName": "loc",
"locationCode": "LOC",
"locationNameLanguage": "EN"
}
},
"codeContext": "ASD",
"language": "EN",
"locationCode": "LOC"
},
"sector": "ASD",
"accumulatedDuration": ""
},
"marriageGroup": "-1"
}
],
"boundCode": "Outbound"
}
]
}
]
},
JSON is a very simple format and easy to read. There are only two different collection types and four value types.
{} is a dictionary and is subscripted by key
[] is an array and is subscripted by index (zero-based)
Everything in double quotes is String, even "123" and "false"
false and true is Bool
Numeric values are Double, without fractional digits Int
<null> is NSNull
With SwiftyJSON you get distance in flightSegment with (root represents the top level object`)
root["availabilityResultList"][0]["availabilityRouteList"][0]["availabilityByDateList"][0]["originDestinationOptionList"][0]["fareComponentGroupList"][0]["boundList"][0]["availFlightSegmentList"][0]["flightSegment"]["distance"]
I have one SwiftyJson object.
I can not filter that array. I have tried this solution https://stackoverflow.com/a/37497170/4831567. But my json format is different that's why not working.
[
{
"name": "19860",
"header": {
"start_time": "1519270200",
"end_time": "1519299000",
"state": "lunch"
},
"alerts": "1",
"venue": {
"location": "Delhi, India",
"timezone": "+05:30"
},
"srs_category": [
0,
1
]
},
{
"name": "19861",
"header": {
"start_time": "1519270200",
"end_time": "1519299000",
"state": "Dinner"
},
"alerts": "1",
"venue": {
"location": "Mumbai, India",
"timezone": "+05:30"
},
"srs_category": [
1,
3
]
},
{
"name": "19862",
"header": {
"start_time": "1519270200",
"end_time": "1519299000",
"state": "lunch"
},
"alerts": "1",
"venue": {
"location": "Surat, India",
"timezone": "+05:30"
},
"srs_category": [
0,
2
]
}
]
i want to find that object that srs_category contain 1. I know it is possible by looping and condition. But i want via NSPredicate. If it is possible then please help me.
Thank You.
Here is easy way to use SwiftyJSON:
let filtered = JSON(yourArray).arrayValue.filter({
$0["srs_category"].arrayValue.map({ $0.intValue }).contains(1)
})
Use a Swift native function rather than NSPredicate
data represents the Data object received from somewhere
do {
if let json = try JSONSerialization.jsonObject(with:data) as? [[String:Any]] {
let srsCategory1 = json.first(where: { dict -> Bool in
guard let array = dict["srs_category"] as? [Int] else { return false }
return array.contains(1)
})
print(srsCategory1 ?? "not found")
}
} catch {
print(error)
}
If there are multiple items which can match the condition replace first with filter. Then the result is a non-optional array.
I am developing a app for ios and I am trying to get each letter of a member of dictionary which I have defined like this :
var Morse = ["a": "01", "b": "1000", "c": "1010", "d": "100", "e": "0", "f": "0010", "g": "110", "h": "0000", "i": "00", "j": "0111", "k": "101", "l": "0100", "m": "11", "n": "10", "o": "111", "p": "0110", "q": "1101", "r": "010", "s": "000", "t": "1", "u": "001", "v": "0001", "w": "011", "x": "1001", "y": "1011", "z": "1100", "1": "01111", "2": "00111", "3": "00011", "4": "00001", "5": "00000", "6": "10000", "7": "11000", "8": "11100", "9": "11110", "0": "11111", " ": "2"]
So for example, if the user enters a I would like to get "0" and then "1". To do this I use a counter :
var counter = 0
var letter: String = ""
var strings_letter: String = ""
letter = Morse[strings_letter]!
var number = Array(letter)[counter]
But this gives me an issue :
Argument type 'String' does not conform to expect type 'Sequence'
What am I doing wrong?
The characters property of a String instance contains a sequence of the characters contained in the String. You could, for a given key (say "a") re-map the .characters of the corresponding value ("01") to single-character String instances to obtain a String array:
if let charsForKeyA = Morse["a"]?.characters.map({ String($0) }) {
charsForKeyA.forEach { print($0) }
} /* 0
1 */
If I got it right, you want to get an array of characters for the value of the inserted key, based on your example, the output should be like:
"a" => ["0", "1"]
"b" => ["1", "0", "0", "0"]
"c" => ["1", "0", "1", "0"]
and so on...
var Morse = ["a": "01", "b": "1000", "c": "1010", "d": "100", "e": "0", "f": "0010", "g": "110", "h": "0000", "i": "00", "j": "0111", "k": "101", "l": "0100", "m": "11", "n": "10", "o": "111", "p": "0110", "q": "1101", "r": "010", "s": "000", "t": "1", "u": "001", "v": "0001", "w": "011", "x": "1001", "y": "1011", "z": "1100", "1": "01111", "2": "00111", "3": "00011", "4": "00001", "5": "00000", "6": "10000", "7": "11000", "8": "11100", "9": "11110", "0": "11111", " ": "2"]
let insertedKey = "a"
if let value = Morse[insertedKey] {
let array = Array(value.characters)
// here is your array!
print(array) // ["0", "1"]
}
Apologies if this is not the right forum to ask, I couldn't think of anywhere else to take my question!
I'm trying to get the most recent weather forecast for a location using the following url http://datapoint.metoffice.gov.uk/public/data/val/wxobs/all/json/354379?res=hourly&key=47fbc7c3-dac4-424a-874d-7c8ff5b45b79
I want to get the weather type. The documentation here says that Weather Type would be a code number from 0-30. However the response from the web service (you can see for yourself following the long hyperlink above) is this:
{"SiteRep":{"Wx":{"Param":[{"name":"G","units":"mph","$":"Wind Gust"},{"name":"T","units":"C","$":"Temperature"},{"name":"V","units":"m","$":"Visibility"},{"name":"D","units":"compass","$":"Wind Direction"},{"name":"S","units":"mph","$":"Wind Speed"},{"name":"W","units":"","$":"Weather Type"},{"name":"P","units":"hpa","$":"Pressure"}]},"DV":{"dataDate":"2013-03-15T17:00:00Z","type":"Obs"}}}
Now I have no trouble reading that information into an NSDictionary object in my IOS app. But i just can't see which bit contains a code number from 0-30!
If you're wondering if i'm using the right request url, i'm pretty sure i am, i got it from the documentation here. (scroll down a little ways.)
I'm just stumped on how to extract the weather type code. I might have gotten myself completely confused and be making the wrong request, their documentation is not very user friendly. As a novice i really need help using this service, but i couldn't find any tutorials with google. Really appreciate anyone who takes the time to look! It would be such a help just to get a second opinion.
actually the response you are getting is incomplete ...complete response is as follows
{
"SiteRep": {
"Wx": {
"Param": [
{
"name": "G",
"units": "mph",
"$": "Wind Gust"
},
{
"name": "T",
"units": "C",
"$": "Temperature"
},
{
"name": "V",
"units": "m",
"$": "Visibility"
},
{
"name": "D",
"units": "compass",
"$": "Wind Direction"
},
{
"name": "S",
"units": "mph",
"$": "Wind Speed"
},
{
"name": "W",
"units": "",
"$": "Weather Type"
},
{
"name": "P",
"units": "hpa",
"$": "Pressure"
}
]
},
"DV": {
"dataDate": "2012-12-10T13:00:00Z",
"type": "Obs",
"Location": {
"i": "3772",
"lat": "51.479",
"lon": "-0.449",
"name": "HEATHROW",
"country": "ENGLAND",
"continent": "EUROPE",
"Period": [
{
"type": "Day",
"value": "2012-12-09Z",
"Rep": [
{
"D": "W",
"P": "1016",
"S": "14",
"T": "9.0",
"V": "40000",
"W": "7",
"$": "840"
},
{
"D": "W",
"P": "1016",
"S": "14",
"T": "9.0",
"V": "40000",
"W": "7",
"$": "900"
},
{
"D": "W",
"P": "1016",
"S": "10",
"T": "8.6",
"V": "45000",
"W": "7",
"$": "960"
},
{
"D": "W",
"P": "1016",
"S": "10",
"T": "8.2",
"V": "40000",
"W": "7",
"$": "1020"
},
{
"D": "WNW",
"P": "1016",
"S": "9",
"T": "7.9",
"V": "40000",
"W": "7",
"$": "1080"
},
{
"D": "WNW",
"P": "1016",
"S": "10",
"T": "7.1",
"V": "30000",
"W": "0",
"$": "1140"
},
{
"D": "WNW",
"P": "1016",
"S": "10",
"T": "6.5",
"V": "28000",
"W": "0",
"$": "1200"
},
{
"D": "WNW",
"P": "1017",
"S": "11",
"T": "6.2",
"V": "27000",
"W": "0",
"$": "1260"
},
{
"D": "W",
"P": "1017",
"S": "8",
"T": "5.2",
"V": "26000",
"W": "0",
"$": "1320"
},
{
"D": "WNW",
"P": "1017",
"S": "9",
"T": "4.8",
"V": "25000",
"W": "0",
"$": "1380"
}
]
},
{
"type": "Day",
"value": "2012-12-10Z",
"Rep": [
{
"D": "WNW",
"P": "1017",
"S": "8",
"T": "4.6",
"V": "23000",
"W": "0",
"$": "0"
},
{
"D": "WNW",
"P": "1017",
"S": "8",
"T": "4.1",
"V": "22000",
"W": "0",
"$": "60"
},
{
"D": "WNW",
"P": "1018",
"S": "9",
"T": "3.9",
"V": "22000",
"W": "0",
"$": "120"
},
{
"D": "NW",
"P": "1018",
"S": "9",
"T": "3.8",
"V": "24000",
"W": "0",
"$": "180"
},
{
"D": "NW",
"P": "1018",
"S": "10",
"T": "3.3",
"V": "26000",
"W": "0",
"$": "240"
},
{
"D": "NW",
"P": "1019",
"S": "9",
"T": "3.1",
"V": "30000",
"W": "0",
"$": "300"
},
{
"D": "NW",
"P": "1019",
"S": "9",
"T": "2.9",
"V": "30000",
"W": "0",
"$": "360"
},
{
"D": "NW",
"P": "1020",
"S": "8",
"T": "2.8",
"V": "28000",
"W": "0",
"$": "420"
},
{
"D": "NW",
"P": "1020",
"S": "6",
"T": "2.4",
"V": "19000",
"W": "0",
"$": "480"
},
{
"D": "NW",
"P": "1021",
"S": "8",
"T": "2.8",
"V": "25000",
"W": "1",
"$": "540"
},
{
"D": "NNW",
"P": "1022",
"S": "9",
"T": "3.9",
"V": "26000",
"W": "1",
"$": "600"
},
{
"D": "NNW",
"P": "1022",
"S": "8",
"T": "4.7",
"V": "25000",
"W": "1",
"$": "660"
},
{
"D": "N",
"P": "1022",
"S": "9",
"T": "5.5",
"V": "27000",
"W": "1",
"$": "720"
},
{
"D": "N",
"P": "1023",
"S": "10",
"T": "5.6",
"V": "30000",
"W": "3",
"$": "780"
}
]
}
]
}
}
}
}