SwiftyJSON对象返回字符串 [英] SwiftyJSON object back to string

查看:146
本文介绍了SwiftyJSON对象返回字符串的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用SwiftyJSON库将JSON解析为swift对象。我可以创建JSON对象并读取和写入它

I'm using the SwiftyJSON library to parse JSON into swift objects. I can create the JSON object and read and write to it

// Create json object to represent library
var libraryObject = JSON(["name":"mylibrary","tasks":["Task1","Task2","Task3"]])


    // Get
    println(libraryObject["name"])
    println(libraryObject["tasks"][0])

    // Set
    println("Setting first task to 'New Task'")
    libraryObject["tasks"][0] = "New Task"

    // Get
    println(libraryObject["tasks"][0])

    // Convert object to JSON and print
    println(libraryObject)

所有这些都按预期工作。我只想将libraryObject转换回JSON格式的字符串!

All of this works as expected. I just want to convert the libraryObject back to a string in JSON format!

println(libraryObject)命令将我想要的内容输出到控制台但我找不到把它作为一个字符串的方式。

The println(libraryObject) command outputs what I want to the console but I can't find a way to get it as a string.

libraryObject.Stringvalue和libraryObject.String都返回空值但是当我尝试例如println(content:+ libraryObject)时,我在尝试追加String时遇到错误一个JSON

libraryObject.Stringvalue and libraryObject.String both return empty values but when I try eg println("content: "+libraryObject) I get an error trying to append a String to a JSON

推荐答案

来自SwiftyJSON的README 在GitHub上

From the README of SwiftyJSON on GitHub:

//convert the JSON to a raw String
if let string = libraryObject.rawString() {
//Do something you want
  print(string)
}

这篇关于SwiftyJSON对象返回字符串的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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