SwiftyJson能够将自定义swift类转换为json字符串吗? [英] Is SwiftyJson capable of converting a custom swift class to a json string?

查看:183
本文介绍了SwiftyJson能够将自定义swift类转换为json字符串吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

以下是我的自定义快速课程。我的问题是如何使用SwiftyJson将此类的对象转换为json字符串?

Given below is my custom swift class. My Question is how to convert an object of this class to a json string using SwiftyJson?

class Equipment{

    var UniqueItemId:String? = ""
    var ItemNo:String? = ""
    var EquipmentType:String? = ""
    var EquipmentDescription:String? = ""
    var Length:String? = ""
    var Wll:String? = ""
    var  EquipmentLocation:String? = ""
    var EquipmentManufacture:String? = ""
    var SerialNo:String? = ""
    var Condition:String? = ""
    var Remarks:String? = ""
    var InspectionDate:String? = ""
    var Inspector:String? = ""

}

例如,像这样:

var jsonString = JSON(equipmentObject);


推荐答案

更新

OP现在对 EVReflection 感到满意。所以我认为现在这是最好的选择。

OP is now happy with EVReflection. So I assume that at this moment it's the best choice.

原始答案

由于Swift反射的可能性尚不丰富,因此现在没有像 google-gson for Java那样的最终解决方案。

Since Swift reflection possibilities is not yet such rich there is no ultimate solution now like google-gson for Java.

SwiftJSON Swift ObjectMapper 只是加糖 NSJSONSerialization ,并要求您定义从json字段到对象属性的映射。

Libraries like SwiftJSON and Swift ObjectMapper are just sugared NSJSONSerialization and require you to define mappings from json fields to object properties.

但事情正在发生变化,你可以尝试 JsonSerializerSwift 。我还没有使用它,但它使用Swift反射,似乎工作正常。

But things are changing and you can try out JsonSerializerSwift. I have not yet used it but it use Swift reflection and seems to work fine.

还可以看看 Swift Mirrors和JSON by Chris Eidhof 文章。它解释了如何使用反射可能性的JSON序列化。

Also check out Swift Mirrors and JSON by Chris Eidhof article. It explains how JSON serialization using reflection possibilities works.

这篇关于SwiftyJson能够将自定义swift类转换为json字符串吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆