Display content of JSON files in multiple lines, instead of one long line

守給你的承諾、 提交于 2021-01-25 07:11:20

问题


In Unity, I save my game using JSON files. When I open the file in Visual Studio, it shows the whole content with all its variables in one single line. Here is a small part of my JSON file:

// JSON before my copy / paste-trick (see below)
{"lastTicketDate":"04.13.2020","lastTicket":{"Id":2,"Type":0,"Fortune":"Fortune02","Author":"Me!!!\r","ShinyEffect":0,"BeenDrawn":true},"firstStart":false,"tickets":[{"Id":1,"Type":0,"Fortune":"Fortune01","Author":
// (...)

This is not very readable. How can I set up Visual Studio to show me the content properly, each variable in a separate line, like this:

// JSON after my copy / paste trick (see below)
{
    "lastTicketDate": "04.13.2020",
    "lastTicket": {
        "Id": 2,
        "Type": 0,
        "Fortune": "Fortune02",
        "Author": "Me!!!\r",
        "ShinyEffect": 0,
        "BeenDrawn": true
    },
    "firstStart": false,
    "tickets": [
        {
            "Id": 1,
            "Type": 0,
            "Fortune": "Fortune01",
            "Author": "Me!!!\r",
            "ShinyEffect": 0,
            "BeenDrawn": false
        },
// (...)

Currently, I do it like this: Double-click on one word -> copy it (Ctrl+c) -> paste it back in (Ctrl+v) -> now the format changed to the desired version.

How do I fix this issue, what is the correct way to do it?


回答1:


Right now when saving you output your json in the smallest size format, which is unindented, so that is why everything is so compressed.

You can instead, change your json serializer to output an idented format.

e.g. when using the builtin json util;

https://docs.unity3d.com/ScriptReference/JsonUtility.ToJson.html

var indented = JsonUtility.ToJson(this, true);


来源:https://stackoverflow.com/questions/61191356/display-content-of-json-files-in-multiple-lines-instead-of-one-long-line

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