首页 > 解决方案 > 如何在 Swift 中基于另一个 JSON 访问一个 JSON 中的信息

问题描述

我有两个 JSON 文件。一种是设备列表,其值为“error”,其中包含错误代码。我想使用此错误代码将其与成员具有值“id”的其他 JSON 进行比较,以便我可以打印存储在错误 JSON 中的其他错误信息。我一直在试图弄清楚如何从 ErrorCodes JSON 中获取信息,但我什至不确定如何准确地表达我的问题。

DeviceList.json 是第一个带有错误值/ID 的 JSON。它有一个值“error”,作为它抛出错误的 ID。

ErrorCodes.json 是第二个 JSON,其中包含我想要访问的其他信息。每个成员都有一个值“id”,它应该与 DeviceList 中的错误值匹配,我想提取一个名为“desc”的值。

非常感谢!

编辑:在 JSON 文件中添加了我想要/寻找的值的名称。

import SwiftUI

struct DeviceDetail: View {
var deviceList: DeviceList

var body: some View {
    var errorID = deviceList.error // Find Error Code from Device
    var errorName = ErrorCodes.id// Finds error Name
    var errorDesc = // Finds error description
    
    VStack {
        ZStack {
            Image("greenBackground")
                .resizable()
                .ignoresSafeArea(edges: .top)
                .frame(height: 100)
            VStack {
                
                //Alert bar for system notifications
                if deviceList.status == false {
                    Text("Error: \(errorName).")
                        .frame(width: 600, height: 20, alignment: .center)
                        .background(Color.yellow)
                        .offset(y: -15)
                    Text("\(errorDesc)")
                        .frame(width: 600, height: 20, alignment: .center)
                        .background(Color.yellow)
                        .offset(y: -15)
                } // If
                
                Text(deviceList.name)
                    .font(.largeTitle)
                    .fontWeight(.heavy)
                    .shadow(radius: 1)
            } // VStack
        }
        
        ZStack {
            Image("backgroundTransition")
                .resizable()
                .offset(y: -10)
                .frame(height: 100)
                .offset(y: -2)
        }
        
        //Timer. For now, it will be held in place by a circle. This will become a timer later with a display in the middle for the time remaining til the next watering
        ZStack {
            CircleImage()
                .frame(height: 300)
            VStack {
                Text("00:00:00") //Insert Timer Display Here
                    .font(.title)
                Text("Til Next Watering")
                    .font(.caption)
            }
        }
        //Divider()
        //Buttons
        HStack {
            WateringSchedule()
                .padding()
            TankStatus()
                .padding()
            
        }
        Spacer()
    }
}
}

struct DeviceDetail_Previews: PreviewProvider {
static var previews: some View {
    DeviceDetail(deviceList: deviceLists[0])
}
}

错误代码.json

[
{
  "id": 0,
  "name": "Low Water",
  "desc": "The water needs to be refilled soon."
},
{
  "id": 1,
  "name": "Low Battery",
  "desc": "The batteries need to be changed soon."
},
{
  "id": 2,
  "name": "Device 3",
  "desc": "Other junk"
}
]

设备列表.json

[
{
  "id": 1001,
  "name": "Device 1",
  "hours": 0,
  "mins": 0,
  "secs": 0,
  "status": false,
  "activated": true,
  "error": 0
},
{
  "id": 1002,
  "name": "Device 2",
  "hours": 0,
  "mins": 0,
  "secs": 0,
  "status": false,
  "activated": true,
  "error": 1
},
{
  "id": 1003,
  "name": "Device 3",
  "hours": 0,
  "mins": 0,
  "secs": 0,
  "status": true,
  "activated": true,
  "error": 0
}
]

标签: jsonswift

解决方案


推荐阅读