How can you subset a SwiftyJSON JSON object

眉间皱痕 提交于 2019-12-02 18:22:22

问题


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":[
    ],    
}

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.


回答1:


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

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

Example:

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

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



来源:https://stackoverflow.com/questions/32219237/how-can-you-subset-a-swiftyjson-json-object

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!