如何子集一个SwiftyJSON JSON对象 [英] How can you subset a SwiftyJSON JSON object

查看:75
本文介绍了如何子集一个SwiftyJSON JSON对象的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在构建一个iOS应用,其中我的一个API调用返回一个大的JSON Blob,我使用SwiftyJSON将其加载到JSON对象中.例如,看起来像这样

I am building an iOS app in which one of my API calls returns a large JSON blob that I load into a JSON object using SwiftyJSON. For example, it looks something like this

{
  "data": {
    "name":"object name",
    "id":1,
    "description":"short description of object",
    "type":"type",
    "runs":[
    ],    
}

作为应用程序的一部分,用户可以修改名称等内容,但是PATCH调用的API端点需要删除runs键.有谁知道如何获取SwiftyJSON JSON对象并创建一个包含键子集的新对象.例如,我希望JSON Blob看起来像

As part of the app the user can modify things like the name, but the API endpoint for the PATCH call needs to have the runs key removed. Does anyone know how to take a SwiftyJSON JSON object and create a new one that has a subset of keys. For example, I want the JSON blob to look like

{
  "data": {
    "name":"object name",
    "id":1,
    "description":"short description of object",
    "type":"type"   
}

我花了很多时间尝试各种事情,但是没有运气.任何帮助将不胜感激.

I have spent many hours trying various things with no luck. Any help would be greatly appreciated.

推荐答案

您可以从SwiftyJSON对象中获取关联的dictionaryValue并使用removeValueForKey方法.

You can grab the associated dictionaryValue from your SwiftyJSON object and use the removeValueForKey method.

当然,这意味着您必须将dictionaryValue分配给变量,您不能在SwiftyJSON对象本身中删除键.

Of course it means you have to assign the dictionaryValue to a variable, you can't remove a key in the SwiftyJSON object itself.

示例:

var dataDict = json["data"].dictionaryValue
dataDict.removeValueForKey("runs")

注意:removeValueForKey方法名称有点误导;它将删除键,而不仅仅是值.

Note: the removeValueForKey method name is a bit misleading; it will remove the key, not just the value.

这篇关于如何子集一个SwiftyJSON JSON对象的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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