在Swift中从类创建JSON对象

3

我是iOS开发和Swift的新手(请谅解)。我有一个像这样定义的类对象:

class LocationPoint {
    var x: Double
    var y: Double
    var orientation: Double

    init(x: Double, y: Double, orientation: Double) {
        self.x = x
        self.y = y
        self.orientation = orientation
    }
}

在我的代理中,我创建了一个类的实例并将其附加到一个数组中(在代理外声明):
var pt = LocationPoint(x: position.x, y: position.y, orientation: position.orientation)
self.LocationPoints.append(pt)

目前为止一切顺利。我可以在我的视图控制器中将数组值显示在文本视图对象中,并且每次更新时它肯定会添加值。

现在,当数组计数达到限制(比如100个值)后,我想要的是将其打包成JSON对象,并使用HTTP请求将其发送到Web服务器。我的最初想法是使用SwiftyJSONAlamofire来帮助解决这个问题...但是如果我尝试将问题分解为更小的部分,则需要:

  1. 从LocationPoints数组创建JSON对象
  2. 创建HTTP请求以将JSON数据包发送到Web服务器

现在,我只是尝试解决第1步,但似乎无法开始。我已经使用CocoaPods安装了这两个库(SwiftyJSON和Alamofire),但我不知道如何在我的viewcontroller.swift文件中实际使用它们。有人能提供一些关于如何从自定义类结构创建JSON对象的指导吗?

2个回答

7

您应该在这里查看[NSJSONSerialization]类的文档

class LocationPoint {
    var x: Double
    var y: Double
    var orientation: Double

    init(x: Double, y: Double, orientation: Double) {
        self.x = x
        self.y = y
        self.orientation = orientation
    }
}

func locationPointToDictionary(locationPoint: LocationPoint) -> [String: NSNumber] {
    return [
        "x": NSNumber(double: locationPoint.x),
        "y": NSNumber(double: locationPoint.y),
        "orientation": NSNumber(double: locationPoint.orientation)
    ]
}

var locationPoint = LocationPoint(x: 0.0, y: 0.0, orientation: 1.0)
var dictPoint = locationPointToDictionary(locationPoint)

if NSJSONSerialization.isValidJSONObject(dictPoint) {
    print("dictPoint is valid JSON")

    // Do your Alamofire requests

}

这比SwiftyJSON更好吗?这种方法在Swift中有任何问题吗(因为我读到在Swift中创建JSON对象比Objective-C更难)? - andyopayne
我认为使用哪种框架并不重要,NSJSONSerialization是自iOS 5起由Apple提供的框架。无论您使用Swift还是Obj-C也都没有关系。您应该阅读文档,因为创建JSON所需的对象有一些要求。 - Marius Fanu
你的意思是我应该使用字典而不是数组来存储LocationPoints(它的类型是LocationPoint)。还是说你的类(LocationPoint)定义应该更改为包含字典而不是单个属性值? - andyopayne
1
你可以创建一个此类型的字典 [String: AnyObject] - Marius Fanu
我保证,这是最后一个。你提供的函数将单个位置点转换为字典,然后我可以用Alamofire封装它...但理想情况下,我不想那么快推送数据(我的更新大约每秒一次)。相反,我想创建一个位置点的集合,如果达到限制(100),则处理该集合,然后发送出去。但是,我在如何为集合(位置点数组)创建字典对象上遇到了问题。我是否只是错过了一些简单的东西? - andyopayne
显示剩余10条评论

1
为了补充Marius的回答,我稍微修改了代码以将一组位置点转换为有效的JSON对象。上面的答案适用于单个点,但是该函数可用于将点数组转换为JSON对象。
func locationPointsToDictionary(locationPoints: [LocationPoint]) -> [Dictionary<String, AnyObject>] {
    var dictPoints: [Dictionary<String, AnyObject>] = []
    for point in locationPoints{
        var dictPoint = [
            "x": NSNumber(double: point.x),
            "y": NSNumber(double: point.y),
            "orientation": NSNumber(double: point.orientation),
            "timestamp": NSString(string: point.timestamp)
        ]
        dictPoints.append(dictPoint)
    }
    return dictPoints
}

然后,在代码中你可以像这样使用它:
var pt = LocationPoint(x: position.x, y: position.y, orientation: position.orientation, timestamp: timeStamp)
self.LocationPoints.append(pt)

if LocationPoints.count == 100 {
    var dictPoints = locationPointsToDictionary(self.LocationPoints)
    if NSJSONSerialization.isValidJSONObject(dictPoints) {
        println("dictPoint is valid JSON")

         // Do your Alamofire requests
    }
    //clear array of Location Points and start over
    LocationPoints = []
}

只有在记录了100个位置点后,才应该打包JSON对象。希望这可以帮到你。


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接