-1

json好的,我需要在文件的某一行添加一行文本

例子:

"Forge": {
        "name": "Forge",
        "lastVersionId": "1.6.4-Forge9.11.1.916"
        },

进入某行的现有json文件有可能吗?

4

3 回答 3

0

是的你可以。尝试使用 unix 命令“sed”。在您的情况下,使用以下脚本将其插入第 5 行:

sed -i '5i "Forge": { "name": "Forge", "lastVersionId": "1.6.4-Forge9.11.1.916" }' Json.file
于 2013-10-10T14:25:34.793 回答
0
@echo on&setlocal enabledelayedexpansion
set NLM=^



set NL=^^^%NLM%%NLM%^%NLM%%NLM%

set a=
set b=0
for /f "delims==" %%i in (file.txt) do (
   set /a b+=1
   set a=!a!%%i!nl!
   if "!b!" EQU "2" (set a=!a!"Forge": {!nl!        "name": "Forge",!nl!            "lastVersionId": "1.6.4-Forge9.11.1.916"!nl!        },!nl!)
)
(echo %a%)>>newfile.txt

在行后插入,请参阅编辑历史;)

于 2013-10-10T14:47:43.987 回答
0

中使用适当的 JSON 解析器来添加foobarkey 的值lastVersionId

use strict; use warnings;
use JSON;
my $json = <<EOF;
{
    "Forge": {
        "name": "Forge",
        "lastVersionId": "1.6.4-Forge9.11.1.916"
        }
}
EOF
my $perl_DS = decode_json $json;
$perl_DS->{Forge}->{lastVersionId} .= " foobar";
print encode_json $perl_DS;

输出 :

$ perl script.pl | json_pp
{
   "Forge" : {
      "name" : "Forge",
      "lastVersionId" : "1.6.4-Forge9.11.1.916 foobar"
   }
}
于 2013-10-12T19:01:12.183 回答