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
0 answers

How do I set a default value to optional parameter in my struct model class in Swft?

I'm working with a model - struct Planet : Codable { var name : String var distance : Int var isSelected : Bool? } The data is being fetched from the server and only name and distance are provided by the server. I have added isSelected…
Love Kumar
  • 48
  • 6
0
votes
2 answers

Getting JSON data out of local scope in SWIFT 4

So, I have been trying to make this code work for a couple of days and I don't know what I am doing wrong. I am getting a JSON from the web and parsing it with Struct blocks. The question is: I want to use the Dictionaries and Array from the JSON…
0
votes
2 answers

Unable to get Dictionary from JSON

This is the code I am using but I am unable to fetch the JSON. Error message: Expected to decode Dictionary but found an array instead. func getMovieByName(completion: @escaping (Bool, Any?, Error?) -> Void) { guard let url =…
BigMac
  • 49
  • 6
0
votes
1 answer

how to handle the nil value variables

I have model as below. struc Info: Decodable { var firstName: String? var lastName: String? } While displaying in tableview cell, what I am doing is as below. personName.text = "\(personArray[indexPath.row].firstName!)…
Fahim Parkar
  • 30,974
  • 45
  • 160
  • 276
0
votes
1 answer

Error Serialization

Why do I can't access all the properties inside of "data" struct by doing this way? Otherwise, I don't know how to parse all the data inside of "Data" array. This is how I'm trying to serialize: import Foundation import Alamofire struct…
0
votes
2 answers

I'm trying to Decode a JSON file locally in my program, but keep getting Error

I'm trying to use JSON Decoder in Swift 4.1 but I keep getting "The data couldn’t be read because it isn’t in the correct format" and I have no idea why. I'm calling a JSON file from the Bundle.main.path and then setting that to a variable after…
Simon McNeil
  • 293
  • 1
  • 13
0
votes
1 answer

What parsing object when property can be integer or bool?

Sometimes server sends me property as bool (true, false). Sometimes server sends me property as an integer (0,1). How can I decodable this case via standard Decodable in Swift 4? Example. I have: final class MyOffer : Codable { var id = 0 …
Yurii Radchenko
  • 343
  • 2
  • 11
0
votes
1 answer

How to receive a json object filled up with angularjs and sent by ajax to server side (PHP)?

I have the following json object: $scope.sellAccessories= [ {"id": "1","name": "aa", "quantity": "3","total_price": "100"} , {"id": "2","name": "bb", "quantity": "4","total_price": "200"} , {"id": "3","name": "cc", "quantity":…
Ali
  • 1,633
  • 7
  • 35
  • 58
0
votes
1 answer

Getting error when receiving JSON after post method in swift

I am sending a POST request to my backend however i am getting this error: The given data was not valid JSON.", underlyingError: Optional(Error Domain=NSCocoaErrorDomain Code=3840 "JSON text did not start with array or object and option to allow …
EmbeddedOS
  • 173
  • 6
  • 18
0
votes
2 answers

Swift 4: JSONDecoder fails in one specific case - "The operation could not be completed"

I am receiving JSON text, converting it to Data, and then using JSONDecoder to create a concrete type represented by the JSON text/string. It does work with my "complex" data structure (which implements Codable), or even a simple array of Int as…
0
votes
0 answers

Decoding JSON using decoder

I'm following a tutorial on the new Codable protocol in Swift 4. I'm retrieving JSON from iTunes. I've setup my Data Model in a file (SearchResult.swift) that conforms to Codable: class ResultArray: Codable { var resultCount = 0 var results…
Martin Muldoon
  • 3,388
  • 4
  • 24
  • 55
0
votes
0 answers

JSONDecoder changing order of elements

Situation I'm trying to decode a simple JSON object of the form: { "firstArray": [ "element1", "element2" ], "secondArray": [ "elementA", "elementB", "elementC" ], "*": [ …
flohei
  • 5,248
  • 10
  • 36
  • 61
0
votes
1 answer

SWift 4 JSONDecoder decode from Alamofire response

I have a structure that contains another structures and arrays. public struct Report: Codable { let s:Student; let vio:[VIO]; let stuPresence: [StuPresence]; } I am trying new JSONDecoder() to transform alamofire response into my struct.…
AlexP
  • 449
  • 2
  • 9
  • 25
0
votes
1 answer

Catchable fatal error: in json_decode

I want to eliminate JSON view format and return data like a string with coma. When i use json_decode($row['test_row']) it returns me Catchable fatal error: Object of class stdClass could not be converted to string in C
Michael Shtefanitsa
  • 303
  • 1
  • 4
  • 14
0
votes
1 answer

storing json_decode data from oldest to newest in MySql database

I have this code: $url = 'http://example.com/523223.json?'; $json= file_get_contents($url); $data = json_decode($json); $rows = $data->{'items'}; foreach ($rows as $row) { echo '

'; $title =…

user2682025
  • 656
  • 2
  • 13
  • 39