iOS开发

Formatting the JSON

2022-10-06  本文已影响0人  _浅墨_

Depending on the app you used, it may have formatted the JSON into a more readable form, but as you can see, Xcode has not, so here’s a trick that makes formatting JSON a breeze on any Mac.

Select all the text in the JSON file and copy it. Open Terminal and type in the following line (don’t copy and paste it or you’ll overwrite the JSON that’s in the clipboard).

pbpaste | json_pp | pbcopy

Press Return.

This sequence of three shell commands pastes the clipboard contents to the json_pp command, which “pretty prints” it, then uses pbcopy to copy the neatly formatted JSON back into the clipboard. Under the hood, macOS calls the clipboard the pasteboard which is why it uses pbpaste and pbcopy.

Return to your original SampleData.json file, delete the contents and press Command-V to paste in the pretty printed JSON, then save the file again:

Note: To know more about json_pp, go back to your Terminal, right-click the command and choose Open man Page to open the built-in help page in a new window. You can also type man json_pp, but this shows the information in your working Terminal window. Using a new window makes it easier to read and test the command.

上一篇下一篇

猜你喜欢

热点阅读