首页 > 解决方案 > 格式不正确的 json 文件导致我的终端出现问题

问题描述

为了编辑我的 bash shell 以使其看起来不错,我在文件中乱七八糟。我下载了 bash_it 并按照本教程执行此操作(https://medium.freecodecamp.org/jazz-up-your-bash-terminal-a-step-by-step-guide-with-pictures-80267554cb22)。但是,现在我在 shell 中看到的只是每一行:

Expecting ',' delimiter: line 39 column 2 (char 2196)
Expecting ',' delimiter: line 39 column 2 (char 2196)

然后我去在文件末尾添加一个逗号,我认为是问题的根源(default.json),如下所示:

{
    "name": "Default color scheme for shell prompts",
    "groups": {
        "hostname":         { "fg": "brightyellow", "bg": "mediumorange", "attrs": [] },
        "environment":      { "fg": "white", "bg": "darkestgreen", "attrs": [] },
        "mode":             { "fg": "darkestgreen", "bg": "brightgreen", "attrs": ["bold"] },
        "attached_clients": { "fg": "white", "bg": "darkestgreen", "attrs": [] }
    },
    "mode_translations": {
        "vicmd": {
            "groups": {
        "hostname":         { "fg": "brightyellow", "bg": "mediumorange", "attrs": [] },
        "environment":      { "fg": "white", "bg": "darkestgreen", "attrs": [] },
        "mode":             { "fg": "darkestgreen", "bg": "brightgreen", "attrs": ["bold"] },
        "attached_clients": { "fg": "white", "bg": "darkestgreen", "attrs": [] },

        "gitstatus":                 { "fg": "gray8",           "bg": "gray2", "attrs": [] },
            "gitstatus_branch":          { "fg": "gray8",           "bg": "gray2", "attrs": [] },
            "gitstatus_branch_clean":    { "fg": "green",           "bg": "gray2", "attrs": [] },
            "gitstatus_branch_dirty":    { "fg": "gray8",           "bg": "gray2", "attrs": [] },
            "gitstatus_branch_detached": { "fg": "mediumpurple",    "bg": "gray2", "attrs": [] },
            "gitstatus_tag":             { "fg": "darkcyan",        "bg": "gray2", "attrs": [] },
            "gitstatus_behind":          { "fg": "gray10",          "bg": "gray2", "attrs": [] },
            "gitstatus_ahead":           { "fg": "gray10",          "bg": "gray2", "attrs": [] },
            "gitstatus_staged":          { "fg": "green",           "bg": "gray2", "attrs": [] },
            "gitstatus_unmerged":        { "fg": "brightred",       "bg": "gray2", "attrs": [] },
            "gitstatus_changed":         { "fg": "mediumorange",    "bg": "gray2", "attrs": [] },
            "gitstatus_untracked":       { "fg": "brightestorange", "bg": "gray2", "attrs": [] },
            "gitstatus_stashed":         { "fg": "darkblue",        "bg": "gray2", "attrs": [] },
            "gitstatus:divider":         { "fg": "gray8",           "bg": "gray2", "attrs": [] }
        },  
        "mode_translations": {
            "vicmd": {
                "groups": {
                    "mode": {"fg": "darkestcyan", "bg": "white", "attrs": ["bold"]}
            }
        }
    }
},

然后我在终端中弹出另一个错误

Expecting property name enclosed in double quotes: line 39 column 3 (char 2197)
Expecting property name enclosed in double quotes: line 39 column 3 (char 2197)

所以我添加了双引号,它需要另一个逗号,更多的引号等等......

理想情况下,我只想能够再次使用我的终端,而不会在每一行出现这些 json 错误。

标签: jsonbash

解决方案


网上确实有很多 JSON 验证器,比如这个:https ://jsoncompare.com/#!/simple/

但是,我使用自己的离线 unix 工具jtc,使用调试选项 ( -d),很容易在 JSON 中发现问题所在的位置(因此也可以将该工具用作 JSON 验证器)。在您的情况下,在 JSON 末尾添加逗号是错误的,因为通常任何嵌套的 JSON 都必须使用}或 with关闭]。因此,一旦删除了尾随逗号,该工具就会给出以下输出:

bash $ <default.json jtc -d
.read_inputs(), reading json from <stdin>
.parsejson(), exception locus: ...          }|        }|    }|}|
.location_(), exception spot: --------------------------------->| (offset: 2421)
jtc json exception: unexpected_end_of_string
bash $ 

unexpected_end_of_stringJSON 结尾可能只意味着一件事:缺少括号]}. 在您的 JSON 中,没有缺少未闭合的数组(它们都已关闭[]),因此只缺少闭合的大括号}

最后一一添加直到JSON开始解析(一共添加了2个):

bash $ <default.json jtc -d
.read_inputs(), reading json from <stdin>
.write_json(), outputting json to <stdout>
{
   "groups": {
      "attached_clients": {
         "attrs": [],
         "bg": "darkestgreen",
         "fg": "white"
      },
      "environment": {
         "attrs": [],
         "bg": "darkestgreen",
         "fg": "white"
      },
      "hostname": {
...

推荐阅读