具有多个与一键对应的字典的字典Swift

时间:2018-11-12 04:36:36

标签: ios json swift dictionary swift4.2

我的数据库中有以下格式的数据(这些数据块中至少有100,000个):

{
  "ax" : 1.232,
  "ay" : 1.897,
  "az" : -0.232
  "timestamp" : 151232326789
 }

我要在我的iOS应用程序中以字典的格式检索并解析为类模型。 上面的数据来自传感器MPU6050,与Firebase使用的NodeMCU通信。由于NodeMCU没有自己的时钟,因此使用ServerValue.Timestamp为数据提供时间戳是唯一的选择。因此,我无法正确构造我现在需要在iOS应用程序上格式化的JSON数据。

我正在从数据库中的时间戳记中提取timedate

这是我在前端中需要的格式:

{
  Date1 : {
          Time1 :{ 
                "ax" : 1.232,
                "ay" : 1.897,
                "az" : -0.232
                  },
          Time2 :{ 
                "ax" : 1.232,
                "ay" : 1.897,
                "az" : -0.232
                  },
          Time3 :{ 
                "ax" : 1.232,
                "ay" : 1.897,
                "az" : -0.232
                  },
          Time4 :{ 
                "ax" : 1.232,
                "ay" : 1.897,
                "az" : -0.232
                  }
            }
  Date2 : {...similar as above.}
   }

我尝试使用NSMutableDictionary,但是数据混乱了。有解决方案吗?

4 个答案:

答案 0 :(得分:1)

我将其构造为一个实体,该实体在数组中保存一个日期的传感器数据,并由于字典未排序而在数组中存储该实体的实例。

var allData: [OneDayData]

struct OneDayData {
    var date: Date
    var sensorData: [SensorData]
}

struct SensorData {
    var time: String
    var ax: Double
    var ay: Double
    var az: Double
}

注意,我不知道您如何转换时间戳,因此我只是假设它已转换为日期和字符串

答案 1 :(得分:1)

import Foundation

// define date - time interval for testing
let start = Calendar.current.date(from: DateComponents(year: 2018, month: 1, day: 1))
let end = Calendar.current.date(from: DateComponents(year: 2018, month: 1, day: 3))
let s = start!.timeIntervalSinceReferenceDate
let e = end!.timeIntervalSinceReferenceDate

// generate array of random data with timestamp from defined date - time interval
var data: [[String: Double]] = []
(0..<10).forEach { _ in
    let ax = Double.random(in: -1.0...1.0)
    let ay = Double.random(in: -1.0...1.0)
    let az = Double.random(in: -1.0...1.0)
    // stamp between start and end date, with one second resolution
    let timestamp = TimeInterval.random(in: s...e).rounded()
    let record = ["ax": ax, "ay": ay, "az": az, "timestamp": timestamp]
    data.append(record)
}

print(data as NSArray)有什么用?

(
        {
        ax = "-0.9915295335923959";
        ay = "0.04220588780831558";
        az = "0.04947324263041164";
        timestamp = 536480749;
    },
        {
        ax = "0.8339518841345668";
        ay = "-0.8796254984325194";
        az = "0.9274526027609205";
        timestamp = 536596358;
    },
        {
        ax = "0.1892933660394962";
        ay = "0.2786212981444189";
        az = "-0.7010853895168836";
        timestamp = 536453459;
    },
        {
        ax = "-0.3879536539762585";
        ay = "-0.9881198368284949";
        az = "-0.8103733151058379";
        timestamp = 536574669;
    },
        {
        ax = "0.4386373099712233";
        ay = "-0.1082200532953461";
        az = "-0.5452489312143274";
        timestamp = 536515655;
    },
        {
        ax = "0.1021774847462089";
        ay = "-0.6414676993950421";
        az = "0.8826716373674426";
        timestamp = 536455164;
    },
        {
        ax = "0.1877861732407253";
        ay = "-0.6069605631703257";
        az = "-0.3766270018644693";
        timestamp = 536569833;
    },
        {
        ax = "0.3011539934614316";
        ay = "-0.8534914632655413";
        az = "0.652288374381045";
        timestamp = 536477646;
    },
        {
        ax = "-0.3087207248856481";
        ay = "0.566261641115348";
        az = "-0.6320769324182691";
        timestamp = 536563296;
    },
        {
        ax = "-0.5450288945879682";
        ay = "0.6143645223909975";
        az = "-0.8973854689667276";
        timestamp = 536609836;
    }
)

现在,我收到的数据与您收到的格式相同(不是JSON,而是Swift的字典数组),并且有了下一个简单功能的帮助

// extract date and time from timestamp value
func stamp(timestamp: TimeInterval)->(date: String, time: String) {
    let date = Date(timeIntervalSinceReferenceDate: timestamp)
    return (DateFormatter.localizedString(from: date, dateStyle: .short, timeStyle: .none),
    DateFormatter.localizedString(from: date, dateStyle: .none, timeStyle: .medium))
}

我们准备根据需要按日期和时间对数据值进行分组

// group data by date and time, if duplicated timestamp, use last value only
let result = data.reduce(into: [String:[String:[String: Double]]]()) { (result, record) in
    guard let ts = record["timestamp"] else { return }
    var record = record
    record["timestamp"] = nil
    let (date, time) = stamp(timestamp: ts)
    if result[date] == nil {
        result[date] = [time : record]
    } else {
        result[date]?[time] = record
    }
}

查看结果[String:[String:[String: Double]]]显示为NSDictionary的样子

{
    "01/01/2018" =     {
        "00:50:59" =         {
            ax = "0.1892933660394962";
            ay = "0.2786212981444189";
            az = "-0.7010853895168836";
        };
        "01:19:24" =         {
            ax = "0.1021774847462089";
            ay = "-0.6414676993950421";
            az = "0.8826716373674426";
        };
        "07:34:06" =         {
            ax = "0.3011539934614316";
            ay = "-0.8534914632655413";
            az = "0.652288374381045";
        };
        "08:25:49" =         {
            ax = "-0.9915295335923959";
            ay = "0.04220588780831558";
            az = "0.04947324263041164";
        };
        "18:07:35" =         {
            ax = "0.4386373099712233";
            ay = "-0.1082200532953461";
            az = "-0.5452489312143274";
        };
    };
    "02/01/2018" =     {
        "07:21:36" =         {
            ax = "-0.3087207248856481";
            ay = "0.566261641115348";
            az = "-0.6320769324182691";
        };
        "09:10:33" =         {
            ax = "0.1877861732407253";
            ay = "-0.6069605631703257";
            az = "-0.3766270018644693";
        };
        "10:31:09" =         {
            ax = "-0.3879536539762585";
            ay = "-0.9881198368284949";
            az = "-0.8103733151058379";
        };
        "16:32:38" =         {
            ax = "0.8339518841345668";
            ay = "-0.8796254984325194";
            az = "0.9274526027609205";
        };
        "20:17:16" =         {
            ax = "-0.5450288945879682";
            ay = "0.6143645223909975";
            az = "-0.8973854689667276";
        };
    };
}

JSON部分由您决定。

答案 2 :(得分:0)

字典无法解决您的问题。这里需要的是自定义模型的数组。

例如,

struct Time {
    var ax: Float
    var ay: Float
    var az: Float
}

struct Date {
    var times: [Time]
}

let array: [Date] = ... // parse your json into this

您需要将json的解析逻辑写入此结构,该逻辑可以通过多种方式完成,而且我认为这超出了最初的问题范围

答案 3 :(得分:0)

  • jsonResponse是您的数组

            if let dictionary = jsonResponse as? [String : Any]
            {
    
                        let all=dictionary["Date1"] as? [[String : Any]]
                         if let all = all
                         {
    
                            let user:[String:Any]=all[0]["Time1"]! as! [String : Any]
                            let user2:[String:Any]=all[0]["Time2"]! as! [String : Any]
    
                            print(user["ax"] as? String)!,user["ay"] as? String)!,user["az"] as? String)!)                          
                            print(user2["ax"] as? String)!,user2["ay"] as? String)!,user2["az"] as? String)!)                            
    
                         }
    
            }