2

我的 json 看起来像这样

{  
    "group":{  
        "personnel":[  
            {  
                "member":{  
                    "id":"1",
                    "name":"John"
                }
            },
            {  
                "member":{  
                    "id":"2",
                    "name":"Doe"
                }
            }
        ]
    }
}

并且为此的预期输出是

[  
    {  
        "id":"1",
        "name":"John"
    },
    {  
        "id":"2",
        "name":"Doe"
    }
]

但是,也有一些时候 json 是空的,像这样:

{}

为此,我希望输出为

[]

我的规格看起来像这样

"spec":{  
    "group":{  
        "personnel":{  
            "*":{  
                "*":"[]"
            }
        }
    }
}

但这不适用于json为空的第二种情况,它只会返回null。我需要添加什么吗?

4

1 回答 1

1

有点笨拙,但这有效。

规格

[
  {
    "operation": "shift",
    "spec": {
      "group": {
        "personnel": {
          "*": {
            // write to a temp array so that we can 
            //  default it into existence later if needed
            "*": "temp[]"
          }
        }
      }
    }
  },
  {
    "operation": "default",
    "spec": {
      // If "temp" does not exist, make it be an empty array
      "temp": []
    }
  },
  {
    "operation": "shift",
    "spec": {
      // write value at "temp" to the root level
      "temp": ""
    }
  }
]
于 2016-09-23T03:00:36.157 回答