请问swift下,如何可以把类对象转成json 50
如:classuser{varname:Stringvarage:Intinit(_name:String,_age:Int){self.name=_nameself.a...
如:
class user
{
var name:String
var age:Int
init(_name:String,_age:Int)
{
self.name = _name
self.age = _age
}
}
let myuser = user(_name: "abc", _age: 15)
//let isok = NSJSONSerialization.isValidJSONObject(myuser)
//print(isok)
do {
let jsonData = try NSJSONSerialization.dataWithJSONObject(myuser, options: NSJSONWritingOptions())
let str = NSString(data: jsonData, encoding: NSUTF8StringEncoding)
print(str!)
}catch
{
} 展开
class user
{
var name:String
var age:Int
init(_name:String,_age:Int)
{
self.name = _name
self.age = _age
}
}
let myuser = user(_name: "abc", _age: 15)
//let isok = NSJSONSerialization.isValidJSONObject(myuser)
//print(isok)
do {
let jsonData = try NSJSONSerialization.dataWithJSONObject(myuser, options: NSJSONWritingOptions())
let str = NSString(data: jsonData, encoding: NSUTF8StringEncoding)
print(str!)
}catch
{
} 展开
展开全部
let jsonData = try NSJSONSerialization.dataWithJSONObject(myuser, options: NSJSONWritingOptions())
let str = NSString(data: jsonData, encoding: NSUTF8StringEncoding)
print(str!)
很对啊!
let str = NSString(data: jsonData, encoding: NSUTF8StringEncoding)
print(str!)
很对啊!
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
2016-02-14
展开全部
好人,赞一个
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
2016-02-14
展开全部
无可奉告
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询