索引到JSON对象
我有一个JSON对象,形状如下:索引到JSON对象,json,swift,Json,Swift,我有一个JSON对象,形状如下: let allSnippets = [<METDocument key: <collection: snippets, ID: xNmjQH5N3HbomnATk>, fields: { text = "First snippet"; }>, <METDocument key: <collection: snippets, ID: KvQWnWfyto5iXXsni>, fields: {
let allSnippets = [<METDocument key: <collection: snippets, ID: xNmjQH5N3HbomnATk>, fields: {
text = "First snippet";
}>, <METDocument key: <collection: snippets, ID: KvQWnWfyto5iXXsni>, fields: {
text = "Second snippet";
}>]
但是这不起作用。什么类型的
allSnippets
?METDocument
from在哪里?@kametrixom该对象可能是NSArray,也可能是JSON,我不确定。请参阅where部分解释:集合中的所有文档都可以作为NSArray访问:在这种情况下,您不能SwiftyJSON
用于解析JSON文件,除此之外没有其他功能。您不能简单地将一些数据转换为JSON。@kametrixom好的,您对如何将索引转换到字段部分有什么建议吗?我发现allSnippets[1]会给我第二个“document”,但我如何才能只得到字符串“secondSnippet”?
let json = JSON(allSnippets)
if let text = json[1]["text"].string{
print(text)
}