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",
"maxchspeed": 22.08,
"connector": 1,
"description": "AVAILABLE"
},
{
"id": 59986,
"name": "CP2500 - 2",
"maxchspeed": 22.08,
"connector": 2,
"description": "AVAILABLE"
}
]
}
this is my struct :
`
struct Root: Codable {
var id: Int
var name: String
var numberOfConnectors: Int
var connectors: [Connector]
}
struct Connector: Codable {
var id: Int
var name: String
var maxchspeed: Double
var connector: Int
var connectorDescription: String
enum CodingKeys: String, CodingKey {
case id, name, maxchspeed, connector
case connectorDescription = "description"
}
}
I want to parse the element within the [Connector] array but I'm just getting the elements of the Root level :
let jsonData = array.data(using: .utf8)!
let root = try JSONDecoder().decode(Root.self, from: jsonData)
print("\(root.id)")
Any idea how to do this ?
do {
let root = try JSONDecoder().decode(Root.self, from: jsonData)
print("root id : \(root.id)")
root.connectors.forEach {
print("name : \($0.name),"," connector id : \($0.id),","status : \($0.description)");
}
} catch {
print(error.localizedDescription)
}
Related
I'm new to SwiftUI and trying to build a simple single-player Quiz app. I started by following this tutorial and then tried to continue on my own. Currently, the questions are hardcoded in my ViewModel but I'd like to change it to fetching from a local JSON file instead. Anyone that can point me in the right direction?
Here's a part of my ViewModel with the questions as static data:
extension GameManagerVM {
static var questions = quizData.shuffled()
static var quizData: [QuizModel] {
[
QuizModel(
id: 1,
question: "Question title 1",
category: "Sport",
answer: "A",
options: [
QuizOption(id: 11, optionId: "A", option: "A"),
QuizOption(id: 12, optionId: "B", option: "B"),
QuizOption(id: 13, optionId: "C", option: "C"),
QuizOption(id: 14, optionId: "D", option: "D")
]
),
...
}
}
Here's a test I did, but I get the error that Xcode can't decode it.
I replace the above code with this:
extension GameManagerVM {
static var questions = quizData.shuffled()
static var quizData: [QuizModel] = Bundle.main.decode("quizquestions2022.json")
}
And here's the JSON.
[
{
"id": "1",
"question": "Question title 1",
"category": "Sport",
"answer": "A",
"options": [
{
"id": "1001",
"optionId": "A",
"option": "A"
},
{
"id": "1002",
"optionId": "B",
"option": "B"
},
{
"id": "1003",
"optionId": "C",
"option": "C"
},
{
"id": "1004",
"optionId": "D",
"option": "D"
}
]
},
]
Here are my models
struct Quiz {
var currentQuestionIndex: Int
var quizModel: QuizModel
var quizCompleted: Bool = false
var quizWinningStatus: Bool = false
var score: Int = 0
}
struct QuizModel: Identifiable, Codable {
var id: Int
var question: String
var category: String
var answer: String
var options: [QuizOption]
}
struct QuizOption: Identifiable, Codable {
var id: Int
var optionId: String
var option: String
var isSelected: Bool = false
var isMatched: Bool = false
}
When you are decoding, unless you make your own decoding like this sample init from decoder Then all of non-optional the vars or lets in the struct need to be in the json. Your data doesn't have isSelected or isMatched in the options, so those need to be optional
struct QuizOption: Identifiable, Codable {
var id: Int
var optionId: String
var option: String
var isSelected: Bool?
var isMatched: Bool?
to fetch your data from a local JSON file, you could try
this approach, where you need to have a model (QuizModel) to
match the json data in your file. Also I used a class GameManagerVM: ObservableObject
to hold/publish your data as an example:
import SwiftUI
#main
struct TestApp: App {
var body: some Scene {
WindowGroup {
ContentView()
}
}
}
struct ContentView: View {
#StateObject var gameManager = GameManagerVM()
var body: some View {
List {
ForEach(gameManager.quizData) { quiz in
Text(quiz.question)
}
}
}
}
struct QuizModel: Identifiable, Codable {
let id, question, category, answer: String
let options: [QuizOption]
enum CodingKeys: String, CodingKey {
case id, question, category, answer, options
}
}
struct QuizOption: Codable {
let id, optionId, option: String
var isSelected: Bool = false
var isMatched: Bool = false
enum CodingKeys: String, CodingKey {
case id, optionId, option
}
}
class GameManagerVM: ObservableObject {
#Published var questions: [QuizModel] = []
#Published var quizData: [QuizModel] = []
init() {
quizData = Bundle.main.decode("quizquestions2022.json")
questions = quizData.shuffled()
}
}
I writing because I need to search a json-key passed in a function like a string. Do you have any suggestion on how I could implement it? Once I find the key I also need to edit the value. Here there is the code I wrote until now:
JSON:
{
"JSONRoot": {
"version": 1,
"Town": {
"hour": 0,
"latitude": "",
"longitude": 0,
"latitudine": 0
},
"MeasurePoints": {
"MeasurePoint": [
{
"code": "",
"codelocation": "",
}
]
},
"Wakeup": {
"startH": 6,
"startM": 0,
"maxAttempts": 3,
"maxRetry": 10
},
"Config": {
"port": 12345,
"A": {
"writable": true,
"value": 12
},
"B": {
"writable": true,
"value": 8
},
},
"Sales": {
"Stores": {
"Store": [
{
"description": "A description",
"type": "1",
"Floors": {
"basement": true,
"number": 2
},
"Doors": {
"type": "",
"number": 7
},
"Lights": {
"number": 20
}
},
{
"description": "A description",
"type": "4",
"Floors": {
"basement": none,
"number": 1
},
"Doors": {
"type": "",
"number": 4
},
"Lights": {
"number": 8
}
}
]
}
}
}
}
Structs with codable:
// MARK: - JSONConfig
struct JsonConfig: Codable {
let jsonRoot: JSONRoot?
enum CodingKeys: String, CodingKey {
case jsonRoot = "JSONRoot"
}
}
// MARK: - JSONRoot
struct JSONRoot: Codable {
let version: Int?
let measurePoints: MeasurePoints?
let wakeup: Wakeup?
let config: Config?
let sale: Sale?
enum CodingKeys: String, CodingKey {
case version
case measurePoints = "MeasurePoints"
case wakeup = "Wakeup"
case config = "Config"
case sale = "Sale"
}
}
// MARK: - Stores
struct Stores: Codable {
let stores: [Store]?
enum CodingKeys: String, CodingKey {
case stores = "Stores"
}
}
// MARK: - Store
struct Store: Codable {
let storeDescription: String?
let type: Int?
let floors: Floors?
let doors: Doors?
let lights: Lights?
enum CodingKeys: String, CodingKey {
case storeDescription = "description"
case type
case floors = "Floors"
case doors = "Doors"
case lights = "Lights"
}
}
// MARK: - Floors
struct Floors: Codable {
let basement: Bool?
let number: Int?
}
// MARK: - Doors
struct Doors: Codable {
let type: String?
let number: Int?
}
// MARK: - Lights
struct Lights: Codable {
let number: Int?
}
// MARK: - MeasurePoints
struct MeasurePoints: Codable {
let measurePoint: [MeasurePoint]?
enum CodingKeys: String, CodingKey {
case measurePoint = "MeasurePoint"
}
}
// MARK: - MeasurePoint
struct MeasurePoint: Codable {
let code, codeLocation: String?
}
// MARK: - Config
struct Config: Codable {
let port: Int?
let a, b: K?
enum CodingKeys: String, CodingKey {
case port
case a = "A"
case b = "B"
}
}
// MARK: - K
struct K: Codable {
let writable: Bool?
let value: Int?
}
// MARK: - Wakeup
struct Wakeup: Codable {
let startH, startM, maxAttempts, maxRetry: Int?
}
Function to search for a key:
func setKeyValue(jsonKey: String, value: String) {
let decoder = JSONDecoder()
let jsonData = Data(C.jsonString.utf8)
if let jsonResult = try? decoder.decode(JsonConfig.self, from: jsonData) {
// At this point I have the jsonKey = "JSONRoot.Wakeup.maxRetry" but how I can use it to search for
// the key in the jsonResult?
}
}
Obviously I need to create a new struct to edit the json but one step at a time.
Using JSONSerialisation is probably the most straightforward way here
var value: Any?
do {
if let jsonResult = try JSONSerialization.jsonObject(with: data) as? [String: Any] {
let keys = "JSONRoot.Wakeup.maxRetry".split(separator: ".").map {String($0)}
var dict = jsonResult
for i in 0..<keys.count {
if let temp = dict[keys[i]] as? [String:Any] {
dict = temp
continue
}
value = dict[keys[i]]
}
}
} catch {
print(error)
}
Note that this doesn't support arrays but a solution for that is very dependent on how the search key syntax would handle an array
If my thinking is correct as you, you can try with this code.
override func viewDidLoad() {
super.viewDidLoad()
let jsonString = """
{
"JSONRoot": {
"version": 1,
"Town": {
"hour": 0,
"latitude": "",
"longitude": 0,
"latitudine": 0
},
"MeasurePoints": {
"MeasurePoint": [{
"code": "",
"codelocation": ""
}]
},
"Wakeup": {
"startH": 6,
"startM": 0,
"maxAttempts": 3,
"maxRetry": 10
},
"Config": {
"port": 12345,
"A": {
"writable": true,
"value": 12
}
},
"Sales": {
"Stores": {
"Store": [{
"description": "A description",
"type": "1",
"Floors": {
"basement": true,
"number": 2
},
"Doors": {
"type": "",
"number": 7
},
"Lights": {
"number": 20
}
},
{
"description": "A description",
"type": "4",
"Floors": {
"basement": "none",
"number": 1
},
"Doors": {
"type": "",
"number": 4
},
"Lights": {
"number": 8
}
}
]
}
}
}
}
"""
editJson(jsonString)
}
func editJson(_ jsonString: String) {
do{
let jsonData = Data(jsonString.utf8)
var jsonObject = try JSONSerialization.jsonObject(with: jsonData)
parseDict(&jsonObject)
print("jsonObject: \(String(describing: jsonObject))")
}catch let error {
print(error.localizedDescription)
}
}
func parseDict(_ jsonObject: inout Any) {
if let _ = jsonObject as? String {
return
} else if var dictionary = jsonObject as? Dictionary<String, Any> {
for (key, value) in dictionary {
var nextObject = value
parseDict(&nextObject)
if let value = getValueWith(key), let _ = dictionary.removeValue(forKey: key) {
dictionary[key] = value
} else {
dictionary[key] = nextObject
}
}
jsonObject = dictionary
}else if let array = jsonObject as? Array<Any> {
var updatedArray = array
for (index, value) in array.enumerated() {
var nextObject = value
parseDict(&nextObject)
updatedArray[index] = nextObject
}
jsonObject = updatedArray
}
}
func getValueWith(_ key: String) -> String? {
return [
"description" : "Amit (amitpstu1#gmail.com) ... so on"
][key]
}
You can refresh your memory or learn more here:
https://developer.apple.com/documentation/foundation/archives_and_serialization/using_json_with_custom_types
You would be looking at merge json from different depths section. Using encodable extension etc.
You could also look here: In Swift, can one use a string to access a struct property? If you want to roll your own search function, like a modified dfs or something.
I want to decode my user json object, but I have my difficulties with decoding the superior object. The superior object is just another user that stands above the user. The structure looks like this
{
"id": 3,
"username": "a",
"email": "a#abc.com",
"userFunction": "4",
"password": "****",
"superior": {
"id": 2,
"username": "b",
"email": "b#abc.com",
"userFunction": "3",
"password": "****",
"superior": {
"id": 1,
"username": "c",
"email": "c#abc.com",
"userFunction": "1",
"password": "****",
"superior": null,
},
},
}
struct UserStructure: Decodable {
var id: Int64?
var username: String?
var email: String?
var userFunction: UserFunctionStructure?
var password: String?
var superior: UserStructure?
}
func fetchUser(username: String){
let urlString = "http://localhost:8080/rest/user/" + username
let url = URL(string: urlString)!
URLSession.shared.dataTask(with: url) { (data, response, error) -> Void in
if error != nil {
print(error!)
return
}
guard let data = data else {
return
}
do {
let user = try JSONDecoder().decode(UserStructure.self, from: data)
print(user)
} catch let err {
print(err)
}
}.resume()
}
When I set the type of superior to "UserStructure?" I get the error "Value Type 'UserStructure" cannot have a stored property that recursively contains it. I thought about creating a SuperiorStructure but then I would have the same problem a step further.
Like the compiler error message says, structs cannot have properties that contain an instance of themselves. Use class in this case:
class UserStructure: Decodable {
var id: Int64?
var username: String?
var email: String?
var userFunction: UserFunctionStructure?
var password: String?
var superior: UserStructure?
}
Is there anyone can help me fix my model? It seems it does not match with the JSON from API Response.
JSON response from postman
{
"error_code": 0,
"data": [
{
"kode": "001",
"name": "BANK INDONESIA PUSAT JAKARTA"
},
{
"kode": "002",
"name": "PT. BANK RAKYAT INDONESIA (Persero) Tbk."
},
{
"kode": "003",
"name": "BANK EKSPOR INDONESIA"
}
],
"msg": "OK"
}
Last Model Edited:
struct ObjectBank: Codable {
let errorCode: Int
let data: [Bank]
let msg: String
enum CodingKeys : String, CodingKey {
case errorCode = "error_code" , data , msg
}
}
struct Bank: Codable {
let kode: String
let name: String
}
Still got error like this
Store model using alamofire
private static func performRequest<T:Decodable>(route:APIRouter,
decoder: JSONDecoder = JSONDecoder(), completion:#escaping
(Result<T>)->Void) -> DataRequest {
// Alamofire.request(route).responseJSON {
// response in
// print(response)
// }
return Alamofire.request(route).responseJSONDecodable (decoder:
decoder){ (response: DataResponse<T>) in
//print(response)
completion(response.result)
}
}
data is an array not dictionary
let data:[Bank]
//
struct ObjectBank: Codable {
let errorCode: Int
let data: [Bank]
let msg: String
enum CodingKeys : String, CodingKey {
case errorCode = "error_code" , data , msg
}
}
struct Bank: Codable {
let kode: String
let name: String
}
//
do {
let dic = try JSONDecoder().decode(ObjectBank.self,data)
}
catch {
print(error)
}
The structure of your response is ok in principle which you can see using the following Playground:
import Cocoa
let jsonData = """
{
"error_code": 0,
"data": [
{
"kode": "001",
"name": "BANK INDONESIA PUSAT JAKARTA"
},
{
"kode": "002",
"name": "PT. BANK RAKYAT INDONESIA (Persero) Tbk."
},
{
"kode": "003",
"name": "BANK EKSPOR INDONESIA"
}
],
"msg": "OK"
}
""".data(using: .utf8)!
struct ObjectBank: Codable {
let errorCode: Int
let data: [Bank]
let msg: String
enum CodingKeys : String, CodingKey {
case errorCode = "error_code" , data , msg
}
}
struct Bank: Codable {
let kode: String
let name: String
}
do {
let banks = try JSONDecoder().decode(ObjectBank.self, from: jsonData)
print(banks)
} catch {
print(error)
}
This will parse without error. Since I do not know AlamoFire very well I have to assume that there is something going wrong with the type of your completion closure. It will "somehow" have to guess that you want to parse ObjectBank in order to make any sense of your response.
Maybe you would have an easier time with responseData?
I have an issue parsing following JSON, the problem is that it starts with an array. What should be the initial struct where I would identify the array? if there was something like "data" before the array I would create another struct and mention data: [Item]? there but this JSON just starts with array.
[
{
"userId": 1,
"id": 1,
"title": "TEST text"
},
{
"userId": 2,
"id": 2,
"title": "TEST text"
},
{
"userId": 3,
"id": 3,
"title": "TEST text"
}
]
struct Item: Codable {
var userId: Int?
var id: Int?
var title: String?
}
You have to add one more struct:
First Way
struct totalItem: Codable {
var total: [Item]?
}
struct Item: Codable {
var userId: Int?
var id: Int?
var title: String?
}
let myStruct = try JSONDecoder().decode(totalItem.self, from: data)
The second way to do that:
let myStruct = try JSONDecoder().decode([Item].self, from: data )