Questions tagged [jsondecoder]

This tag should be used for `JSONDecoder` (introduced in Swift 4) questions for decoding JSON decoder on Apple platforms.

JSONDecoder is a class, introduced in Swift 4, for decoding JSON on Apple operating systems that conform to Decodable protocol (or Codable protocol, which is type alias for Encodable & Codable). This offers a simple mechanism to easily parse JSON from Decodable Swift types. This applies to standard Swift collections, such as Array and Dictionary, but custom types can conform to Decodable to participate in this simplified JSON decoding process.

This replaces/supplements the JSONSerialiation class used in prior Swift versions.

See also:

528 questions
0
votes
1 answer

JSON Decoding in Swift 4

I am trying to convert the following JSON from the Met Office to a object in Swift 4 but am running in to errors. My plan is to then store in Core Data once the JSON has been decoded. Here is some of the JSON that is returned let json = """ { …
0
votes
0 answers

nothing happening while decoding json data

Im trying to decode the following json data, NOTE, the data has a weird attribute a '\' in front of each…
Elinoter99
  • 599
  • 1
  • 7
  • 22
0
votes
5 answers

Convert nil to empty string using JSONDecoder and Swift

I am using Swift 4 and JSONDecoder. I have the following structure: struct Customer: Codable { var id: Int! var cnum: String! var cname: String! } Note: the fields cannot be made optional. Now I have a JSON string: [ { …
Robert Smith
  • 634
  • 1
  • 8
  • 22
0
votes
1 answer

How to get data from nested JSON using JSONDecoder in swift 4?

I am very new in swift, first of all I want to know what is the major difference between Encoding and Decoding. Secondly, I am try to get a particular value from the response as shown below: I want to decode the value of gender_category and put it…
TheSwiftGuy77
  • 656
  • 1
  • 9
  • 25
0
votes
1 answer

Parse Json and get result in php

I have a json data i am trying to get longitude and latitude from that json. here is the JSON code { "0": { "provider": "gps", "time": 1517235370666, "latitude": 31.501278877258, "longitude": 74.366261959076, "accuracy": 63, …
M Arfan
  • 4,384
  • 4
  • 29
  • 46
0
votes
2 answers

Swift: parsing Date from JSON

i working on project where i have to receive data from php server. Everything works good, only two things: 1.) can't parse Date. I know that this question was answered here, but not one of the variants is working in my case. Here is my code import…
0
votes
0 answers

How to parse json from cURL results?

I'm forced to use cURL to submit a form to obtain the results I need, unfortunately I've been haunted by the tedious task of having to parse the results that are in JSON. My end-goal is to get this into a table that is legible and not some big block…
aaron1312
  • 37
  • 11
-1
votes
1 answer

Decoding an array containing multiple types

Hello does anyone know a nice way to decode the below Json? It is array with different elements in it. I tried to decode it using keyedBy for the container but I could not make it work. For the json example, the first element is a Journey object and…
Luciano Perez
  • 101
  • 1
  • 10
-1
votes
2 answers

Decoding json message inside of string

I have the following decoding function; def flatten_data(json_data): """ Arguments: json_data (dict): json data Returns: dict : {a:1, b:2, b_c:1, b_d:2} """ out = {} def flatten(x, name=''): if…
Ugur Selim Ozen
  • 159
  • 3
  • 10
-1
votes
1 answer

How to decode nested Json with Swift?

I have been trying to decode this Json data but I'm not able to do it completly : This is my sample json data : { "id": 10644, "name": "CP2500", "numberOfConnectors": 2, "connectors": [ { "id": 59985, "name": "CP2500 - 1", …
Stee Ve
  • 3
  • 2
-1
votes
1 answer

JSON Serialization returns 105 bytes but decoding returns Nil

Happy Sunday everyone. I have a problem with decoding my data. I can't seem to decode data from when I serialize dictionary strings into an object. Im trying to fetch matches from firebase, and this works for when I fetch user profiles but not with…
-1
votes
1 answer

How to map a single value from a JSON dictionary to a property when using Swift Decodable

I have a JSON object that looks something like this: { "name": "Acid Arrow", "school": { "name": "Evocation", "url": "http://www.dnd5eapi.co/api/magic-schools/5" } } that I would like to model in Swift as the…
ra9r
  • 4,528
  • 4
  • 42
  • 52
-1
votes
1 answer

Decoding date value from a json and show it as custom string date format in swiftUI

I'm trying to decode a json that has date value inside the json is like this { "id": 2 "startDate": "2022-08-01T18:44:09.538", "endDate": "2023-07-30T03:03:56.249", "dateCreated": "2022-07-30T03:05:08.8338403" } and here is my struct model for…
-1
votes
1 answer

How can I retrieve a JSON property that has a dash in its name in Swift?

I've just come across a problem in Swift that I haven't found a solution for: When you decode JSON data (from an API) in Swift, you have to decode it using JSONDecoder, but this forces me to name my variable name exactly like the JSON property that…
mirco03
  • 27
  • 3
-1
votes
1 answer

How to send tokens with url using URLSession and decode it in SwiftUI

I need to send tokens with a get url in my iOS app and use its response in my view. One can fetch api through the following code: final class IMDBAPI{ // function to get data from the api func getDataForPageNr(page: Int, completion: @escaping…