I am having an issue parsing out a json file with swiftyjson that has two numeric values for the first two elements.
JSON File
[
[
{
"bv": 0,
"ev": 30
},
{
"bv": 31,
"ev": 55
},
{
"bv": 56,
"ev": 79
},
{
"bv": 80,
"ev": 105
},
{
"bv": 106,
"ev": 137
},
{
"bv": 138,
"ev": 159
},
{
"bv": 160,
"ev": 183
},
{
"bv": 184,
"ev": 205
},
{
"bv": 206,
"ev": 234
},
{
"bv": 235,
"ev": 266
},
{
"bv": 267,
"ev": 298
},
{
"bv": 299,
"ev": 318
},
{
"bv": 319,
"ev": 336
},
{
"bv": 337,
"ev": 360
},
{
"bv": 361,
"ev": 381
},
{
"bv": 382,
"ev": 397
},
{
"bv": 398,
"ev": 424
},
{
"bv": 425,
"ev": 457
},
{
"bv": 458,
"ev": 495
},
{
"bv": 496,
"ev": 513
},
{
"bv": 514,
"ev": 547
},
{
"bv": 548,
"ev": 572
},
{
"bv": 572,
"ev": 591
},
{
"bv": 592,
"ev": 658
},
{
"bv": 659,
"ev": 692
},
{
"bv": 693,
"ev": 727
},
{
"bv": 728,
"ev": 773
},
{
"bv": 774,
"ev": 795
},
{
"bv": 796,
"ev": 830
},
{
"bv": 831,
"ev": 873
},
{
"bv": 874,
"ev": 927
},
{
"bv": 928,
"ev": 960
},
{
"bv": 961,
"ev": 980
},
{
"bv": 981,
"ev": 1011
},
{
"bv": 1012,
"ev": 1040
},
{
"bv": 1041,
"ev": 1083
},
{
"bv": 1084,
"ev": 1119
},
{
"bv": 1120,
"ev": 1149
},
{
"bv": 1150,
"ev": 1172
},
{
"bv": 1173,
"ev": 1195
},
{
"bv": 1196,
"ev": 1252
},
{
"bv": 1253,
"ev": 1290
},
{
"bv": 1291,
"ev": 1324
},
{
"bv": 1325,
"ev": 1358
},
{
"bv": 1359,
"ev": 1386
},
{
"bv": 1387,
"ev": 1420
},
{
"bv": 1421,
"ev": 1451
},
{
"bv": 1452,
"ev": 1473
},
{
"bv": 1474,
"ev": 1506
},
{
"bv": 1507,
"ev": 1532
}
],
[
{
"bv": 1533,
"ev": 1554
},
{
"bv": 1555,
"ev": 1579
},
{
"bv": 1580,
"ev": 1601
},
{
"bv": 1602,
"ev": 1632
},
{
"bv": 1633,
"ev": 1655
},
{
"bv": 1656,
"ev": 1685
},
{
"bv": 1686,
"ev": 1714
},
{
"bv": 1715,
"ev": 1742
},
{
"bv": 1743,
"ev": 1777
},
{
"bv": 1778,
"ev": 1806
},
{
"bv": 1807,
"ev": 1816
},
{
"bv": 1817,
"ev": 1967
},
{
"bv": 1868,
"ev": 1989
},
{
"bv": 1890,
"ev": 1920
},
{
"bv": 1921,
"ev": 1947
},
{
"bv": 1948,
"ev": 1983
},
{
"bv": 1984,
"ev": 1999
},
{
"bv": 2000,
"ev": 2026
},
{
"bv": 2027,
"ev": 2051
},
{
"bv": 2052,
"ev": 2077
},
{
"bv": 2078,
"ev": 2114
},
{
"bv": 2115,
"ev": 2146
},
{
"bv": 2145,
"ev": 2177
},
{
"bv": 2178,
"ev": 2195
},
{
"bv": 2196,
"ev": 2235
},
{
"bv": 2236,
"ev": 2272
},
{
"bv": 2273,
"ev": 2293
},
{
"bv": 2294,
"ev": 2336
},
{
"bv": 2337,
"ev": 2382
},
{
"bv": 2383,
"ev": 2420
},
{
"bv": 2421,
"ev": 2438
},
{
"bv": 2439,
"ev": 2473
},
{
"bv": 2474,
"ev": 2496
},
{
"bv": 2497,
"ev": 2531
},
{
"bv": 2532,
"ev": 2566
},
{
"bv": 2567,
"ev": 2604
},
{
"bv": 2605,
"ev": 2633
},
{
"bv": 2634,
"ev": 2664
},
{
"bv": 2665,
"ev": 2707
},
{
"bv": 2708,
"ev": 2745
}
]
]
I am trying to access specific elements knowing what the first two elements are: In this case 0,0 should return 0 and 47, with 1,0 returning 393 and 1298.
My problem is I'm not sure how to structure the lookup.
Here is what I have so far:
...
var myBeg: Int = 0
var myEnd: Int = 0
if let file = Bundle.main.path(forResource: "Blank", ofType: "json") {
jsonData = try? Data(contentsOf: URL(fileURLWithPath: file))
} else {
print("Fail")
}
let jsonString = String(data: jsonData!, encoding: .utf8)
let value = JSON(jsonData)
let currentUserName = jsonData![0]
for i in 0...value.count-1 {
print("Element Value - \(value[oKey][oKey2]["bv"])")
}
}
I know this has to be super simple, I'm just not getting it. I took a look at the samples in the swiftyJSON source but so far haven't come up with an answer.
Any help would be appreciative.
I am having an issue parsing out a json file with swiftyjson that has two numeric values for the first two elements.
JSON File
[
[
{
"bv": 0,
"ev": 30
},
{
"bv": 31,
"ev": 55
},
{
"bv": 56,
"ev": 79
},
{
"bv": 80,
"ev": 105
},
{
"bv": 106,
"ev": 137
},
{
"bv": 138,
"ev": 159
},
{
"bv": 160,
"ev": 183
},
{
"bv": 184,
"ev": 205
},
{
"bv": 206,
"ev": 234
},
{
"bv": 235,
"ev": 266
},
{
"bv": 267,
"ev": 298
},
{
"bv": 299,
"ev": 318
},
{
"bv": 319,
"ev": 336
},
{
"bv": 337,
"ev": 360
},
{
"bv": 361,
"ev": 381
},
{
"bv": 382,
"ev": 397
},
{
"bv": 398,
"ev": 424
},
{
"bv": 425,
"ev": 457
},
{
"bv": 458,
"ev": 495
},
{
"bv": 496,
"ev": 513
},
{
"bv": 514,
"ev": 547
},
{
"bv": 548,
"ev": 572
},
{
"bv": 572,
"ev": 591
},
{
"bv": 592,
"ev": 658
},
{
"bv": 659,
"ev": 692
},
{
"bv": 693,
"ev": 727
},
{
"bv": 728,
"ev": 773
},
{
"bv": 774,
"ev": 795
},
{
"bv": 796,
"ev": 830
},
{
"bv": 831,
"ev": 873
},
{
"bv": 874,
"ev": 927
},
{
"bv": 928,
"ev": 960
},
{
"bv": 961,
"ev": 980
},
{
"bv": 981,
"ev": 1011
},
{
"bv": 1012,
"ev": 1040
},
{
"bv": 1041,
"ev": 1083
},
{
"bv": 1084,
"ev": 1119
},
{
"bv": 1120,
"ev": 1149
},
{
"bv": 1150,
"ev": 1172
},
{
"bv": 1173,
"ev": 1195
},
{
"bv": 1196,
"ev": 1252
},
{
"bv": 1253,
"ev": 1290
},
{
"bv": 1291,
"ev": 1324
},
{
"bv": 1325,
"ev": 1358
},
{
"bv": 1359,
"ev": 1386
},
{
"bv": 1387,
"ev": 1420
},
{
"bv": 1421,
"ev": 1451
},
{
"bv": 1452,
"ev": 1473
},
{
"bv": 1474,
"ev": 1506
},
{
"bv": 1507,
"ev": 1532
}
],
[
{
"bv": 1533,
"ev": 1554
},
{
"bv": 1555,
"ev": 1579
},
{
"bv": 1580,
"ev": 1601
},
{
"bv": 1602,
"ev": 1632
},
{
"bv": 1633,
"ev": 1655
},
{
"bv": 1656,
"ev": 1685
},
{
"bv": 1686,
"ev": 1714
},
{
"bv": 1715,
"ev": 1742
},
{
"bv": 1743,
"ev": 1777
},
{
"bv": 1778,
"ev": 1806
},
{
"bv": 1807,
"ev": 1816
},
{
"bv": 1817,
"ev": 1967
},
{
"bv": 1868,
"ev": 1989
},
{
"bv": 1890,
"ev": 1920
},
{
"bv": 1921,
"ev": 1947
},
{
"bv": 1948,
"ev": 1983
},
{
"bv": 1984,
"ev": 1999
},
{
"bv": 2000,
"ev": 2026
},
{
"bv": 2027,
"ev": 2051
},
{
"bv": 2052,
"ev": 2077
},
{
"bv": 2078,
"ev": 2114
},
{
"bv": 2115,
"ev": 2146
},
{
"bv": 2145,
"ev": 2177
},
{
"bv": 2178,
"ev": 2195
},
{
"bv": 2196,
"ev": 2235
},
{
"bv": 2236,
"ev": 2272
},
{
"bv": 2273,
"ev": 2293
},
{
"bv": 2294,
"ev": 2336
},
{
"bv": 2337,
"ev": 2382
},
{
"bv": 2383,
"ev": 2420
},
{
"bv": 2421,
"ev": 2438
},
{
"bv": 2439,
"ev": 2473
},
{
"bv": 2474,
"ev": 2496
},
{
"bv": 2497,
"ev": 2531
},
{
"bv": 2532,
"ev": 2566
},
{
"bv": 2567,
"ev": 2604
},
{
"bv": 2605,
"ev": 2633
},
{
"bv": 2634,
"ev": 2664
},
{
"bv": 2665,
"ev": 2707
},
{
"bv": 2708,
"ev": 2745
}
]
]
I am trying to access specific elements knowing what the first two elements are: In this case 0,0 should return 0 and 47, with 1,0 returning 393 and 1298.
My problem is I'm not sure how to structure the lookup.
Here is what I have so far:
...
var myBeg: Int = 0
var myEnd: Int = 0
if let file = Bundle.main.path(forResource: "Blank", ofType: "json") {
jsonData = try? Data(contentsOf: URL(fileURLWithPath: file))
} else {
print("Fail")
}
let jsonString = String(data: jsonData!, encoding: .utf8)
let value = JSON(jsonData)
let currentUserName = jsonData![0]
for i in 0...value.count-1 {
print("Element Value - \(value[oKey][oKey2]["bv"])")
}
}
I know this has to be super simple, I'm just not getting it. I took a look at the samples in the swiftyJSON source but so far haven't come up with an answer.
Any help would be appreciative.
Share edited Feb 11 at 6:54 Joakim Danielson 52.1k5 gold badges33 silver badges71 bronze badges asked Feb 11 at 0:01 Douglas W. PalmeDouglas W. Palme 5721 gold badge5 silver badges12 bronze badges2 Answers
Reset to default 2You could try a different approach
to decoding your json data, using a Codable struct.
Example code:
struct TestEntry: Identifiable, Codable, Hashable {
let id = UUID()
let bv, ev: Int // <--- here, add ? if required
enum CodingKeys: String, CodingKey {
case bv, ev // <--- here
}
}
struct ContentView: View {
@State private var entries: [[TestEntry]] = []
var body: some View {
List (entries, id: \.self) { entry in
ForEach(entry) { test in
HStack {
Text("BV: \(test.bv)")
Text("EV: \(test.ev)")
}
}
}
.onAppear {
entries = getData()
}
}
func getData() -> [[TestEntry]] {
if let fileURL = Bundle.main.url(forResource: "Blank", withExtension: "json") {
do {
let data = try Data(contentsOf: fileURL)
// print("---> data:\n \(String(data: data, encoding: .utf8) as AnyObject)")
let results = try JSONDecoder().decode([[TestEntry]].self, from: data)
// print("---> results: \(results)")
// for testing
for i in results.indices {
for j in results[i].indices {
print("---> results[\(i)][\(j)]: bv=\(results[i][j].bv) ev=\(results[i][j].ev)")
}
}
return results
} catch {
print(error)
}
}
return []
}
}
Note, your JSON data consist of an array of arrays of data.
i am sharing same code as @workingdog support Ukraine but try to explain it in simple term.
Create a response model which is suitable to your response and also make sure it contains optional data types so if values are not same ex: if one value is in int and other one is in string that it will consider it as nil.
here i created model according to response json.
struct DemoElement: Codable {
let bv, ev: Int?
}
now read json file and convert that json to data and decode it into the model like given below.
if let url = Bundle.main.url(forResource: "Blank", withExtension: "json") {
do {
let data = try Data(contentsOf: url)
let arrDictModel = try JSONDecoder().decode([[DemoElement]].self, from: data)
for (i,dictModel) in arrDictModel.enumerated() {
for dict in dictModel {
print("Index: \(i) BV: \(dict.bv ?? 0)")
print("Index: \(i) EV: \(dict.ev ?? 0)")
}
}
} catch {
print(error.localizedDescription)
}
}
Result i get(this result is according to your json given in question)
Index: 0 BV: 0
Index: 0 EV: 30
Index: 0 BV: 31
Index: 0 EV: 55
Index: 0 BV: 56
Index: 0 EV: 79
Index: 0 BV: 80
Index: 0 EV: 105
Index: 0 BV: 106
Index: 0 EV: 137
Index: 0 BV: 138
Index: 0 EV: 159
Index: 0 BV: 160
Index: 0 EV: 183
Index: 0 BV: 184
Index: 0 EV: 205
Index: 0 BV: 206
Index: 0 EV: 234
Index: 0 BV: 235
Index: 0 EV: 266
Index: 0 BV: 267
Index: 0 EV: 298
Index: 0 BV: 299
Index: 0 EV: 318
Index: 0 BV: 319
Index: 0 EV: 336
Index: 0 BV: 337
Index: 0 EV: 360
Index: 0 BV: 361
Index: 0 EV: 381
Index: 0 BV: 382
Index: 0 EV: 397
Index: 0 BV: 398
Index: 0 EV: 424
Index: 0 BV: 425
Index: 0 EV: 457
Index: 0 BV: 458
Index: 0 EV: 495
Index: 0 BV: 496
Index: 0 EV: 513
Index: 0 BV: 514
Index: 0 EV: 547
Index: 0 BV: 548
Index: 0 EV: 572
Index: 0 BV: 572
Index: 0 EV: 591
Index: 0 BV: 592
Index: 0 EV: 658
Index: 0 BV: 659
Index: 0 EV: 692
Index: 0 BV: 693
Index: 0 EV: 727
Index: 0 BV: 728
Index: 0 EV: 773
Index: 0 BV: 774
Index: 0 EV: 795
Index: 0 BV: 796
Index: 0 EV: 830
Index: 0 BV: 831
Index: 0 EV: 873
Index: 0 BV: 874
Index: 0 EV: 927
Index: 0 BV: 928
Index: 0 EV: 960
Index: 0 BV: 961
Index: 0 EV: 980
Index: 0 BV: 981
Index: 0 EV: 1011
Index: 0 BV: 1012
Index: 0 EV: 1040
Index: 0 BV: 1041
Index: 0 EV: 1083
Index: 0 BV: 1084
Index: 0 EV: 1119
Index: 0 BV: 1120
Index: 0 EV: 1149
Index: 0 BV: 1150
Index: 0 EV: 1172
Index: 0 BV: 1173
Index: 0 EV: 1195
Index: 0 BV: 1196
Index: 0 EV: 1252
Index: 0 BV: 1253
Index: 0 EV: 1290
Index: 0 BV: 1291
Index: 0 EV: 1324
Index: 0 BV: 1325
Index: 0 EV: 1358
Index: 0 BV: 1359
Index: 0 EV: 1386
Index: 0 BV: 1387
Index: 0 EV: 1420
Index: 0 BV: 1421
Index: 0 EV: 1451
Index: 0 BV: 1452
Index: 0 EV: 1473
Index: 0 BV: 1474
Index: 0 EV: 1506
Index: 0 BV: 1507
Index: 0 EV: 1532
Index: 1 BV: 1533
Index: 1 EV: 1554
Index: 1 BV: 1555
Index: 1 EV: 1579
Index: 1 BV: 1580
Index: 1 EV: 1601
Index: 1 BV: 1602
Index: 1 EV: 1632
Index: 1 BV: 1633
Index: 1 EV: 1655
Index: 1 BV: 1656
Index: 1 EV: 1685
Index: 1 BV: 1686
Index: 1 EV: 1714
Index: 1 BV: 1715
Index: 1 EV: 1742
Index: 1 BV: 1743
Index: 1 EV: 1777
Index: 1 BV: 1778
Index: 1 EV: 1806
Index: 1 BV: 1807
Index: 1 EV: 1816
Index: 1 BV: 1817
Index: 1 EV: 1967
Index: 1 BV: 1868
Index: 1 EV: 1989
Index: 1 BV: 1890
Index: 1 EV: 1920
Index: 1 BV: 1921
Index: 1 EV: 1947
Index: 1 BV: 1948
Index: 1 EV: 1983
Index: 1 BV: 1984
Index: 1 EV: 1999
Index: 1 BV: 2000
Index: 1 EV: 2026
Index: 1 BV: 2027
Index: 1 EV: 2051
Index: 1 BV: 2052
Index: 1 EV: 2077
Index: 1 BV: 2078
Index: 1 EV: 2114
Index: 1 BV: 2115
Index: 1 EV: 2146
Index: 1 BV: 2145
Index: 1 EV: 2177
Index: 1 BV: 2178
Index: 1 EV: 2195
Index: 1 BV: 2196
Index: 1 EV: 2235
Index: 1 BV: 2236
Index: 1 EV: 2272
Index: 1 BV: 2273
Index: 1 EV: 2293
Index: 1 BV: 2294
Index: 1 EV: 2336
Index: 1 BV: 2337
Index: 1 EV: 2382
Index: 1 BV: 2383
Index: 1 EV: 2420
Index: 1 BV: 2421
Index: 1 EV: 2438
Index: 1 BV: 2439
Index: 1 EV: 2473
Index: 1 BV: 2474
Index: 1 EV: 2496
Index: 1 BV: 2497
Index: 1 EV: 2531
Index: 1 BV: 2532
Index: 1 EV: 2566
Index: 1 BV: 2567
Index: 1 EV: 2604
Index: 1 BV: 2605
Index: 1 EV: 2633
Index: 1 BV: 2634
Index: 1 EV: 2664
Index: 1 BV: 2665
Index: 1 EV: 2707
Index: 1 BV: 2708
Index: 1 EV: 2745