php - .txt 写入 .json 文件批处理或需要 php 文件

标签 php json batch-file

我需要并搜索结果以将我的 .txt 文件转换为具有该格式的 .json 文件:

{
"id":"12777475756802056",
"typ":"Solo",
"match":
    {
        "rank":"1",
        "playeruserid":"165496871657",
        "playername":"Example Name",
        "kills":"8",
        "points":"224000",
        "killer":"empty", // while firstplace
        "weapon":"empty" // while firstplace
    },
    {
        "rank":"2",
        "playeruserid":"654987654984",
        "playername":"Example Name 2",
        "kills":"4",
        "points":"168000",
        "killer":"Example Name",
        "weapon":"Shotgun"
    }
    ... another players here.
}

我的 .txt 文件看起来是这样,是的,真的很糟糕......有这么多空格并且全部在一行......: https://pastebin.com/FP0C9BCj

我尝试这批处理来删除许多空格:

@ECHO OFF
SETLOCAL ENABLEDELAYEDEXPANSION 
SET "sourcedir=D:\Users\Raphael\Desktop"
SET "destdir=D:\Users\Raphael\Desktop"
SET "filename1=%sourcedir%\matchreport.txt"
SET "outfile=%destdir%\outfile.txt"
(
FOR /f "usebackqdelims=" %%a IN ("%filename1%") DO (
 SET "line=%%a"
 SET "line=!line:?=!"
 SET "line=!line:/=-!"
 SET "line=!line::=!"
 SET "line=!line: =;!"
 ECHO !line!
)
)>"%outfile%"

GOTO :EOF

我不知道为什么现在可以将其转换为json文件,而所有信息都在一行职业玩家中,并且json格式现在是:

{"1. 5605****11014 Cl***ma  205000 + 19000 ( 8) = 224000"}

抱歉,这是我第一次尝试将 txt 转换为 json 文件,你知道如何转换吗? :/

最佳答案

这几乎可以工作了。 (需要 PowerShell 3.0 或更高版本,因此需要 Windows 8 或更高版本。)它是 Batch + PowerShell 多语言。使用 .bat 扩展名保存它。

<# : batch portion
@echo off & setlocal

set "file=test.txt"

powershell -noprofile "iex (${%~f0} | out-string)"
exit /b

: end batch / begin powershell #>

function Parse-Fields() {
    add-type -as Microsoft.VisualBasic
    $parser = new-object Microsoft.VisualBasic.FileIO.TextFieldParser($env:file)

    $parser.TextFieldType = "FixedWidth"
    $parser.TrimWhiteSpace = $true
    $parser.FieldWidths = @(7, 21, 25, 6, 9, 4, 12, 26, -1)

    while (!$parser.EndOfData) {
        try {
            $parser.ReadFields() -join "," -replace "[\(\+\)\.=]" | ?{ $_ -match "\d" }
        }
        catch {}
    }
    $parser.Close()
}

$header = "ID","GameUserId","Name","Rank","Kills","Score","Total","Killer","Weapon"
Parse-Fields | ConvertFrom-Csv -Header $header | ConvertTo-Json

输出:

[
    {
        "ID":  "1",
        "GameUserId":  "5605****11014",
        "Name":  "Cl***ma",
        "Rank":  "205000",
        "Kills":  "19000",
        "Score":  "8",
        "Total":  "224000",
        "Killer":  "",
        "Weapon":  "m4"
    },
    {
        "ID":  "2",
        "GameUserId":  "238444****020",
        "Name":  "Ap*******ift",
        "Rank":  "172403",
        "Kills":  "11550",
        "Score":  "3",
        "Total":  "183953",
        "Killer":  "Cl***ma",
        "Weapon":  "m4"
    },
    {
        "ID":  "3",
        "GameUserId":  "92******9515",
        "Name":  "Sw****UK",
        "Rank":  "156259",
        "Kills":  "14900",
        "Score":  "6",
        "Total":  "171159",
        "Killer":  "Ap*******ift",
        "Weapon":  "m4"
    },
    {
        "ID":  "4",
        "GameUserId":  "6583833***132",
        "Name":  "Moc********kap",
        "Rank":  "144805",
        "Kills":  "2000",
        "Score":  "1",
        "Total":  "146805",
        "Killer":  "Cl***ma",
        "Weapon":  "shotgun"
    },
    {
        "ID":  "5",
        "GameUserId":  "621***7360388",
        "Name":  "Ol***r***",
        "Rank":  "135920",
        "Kills":  "6200",
        "Score":  "3",
        "Total":  "142120",
        "Killer":  "Ap*******ift",
        "Weapon":  "m4"
    },
    {
        "ID":  "6",
        "GameUserId":  "189661****980",
        "Name":  "Op*********gon",
        "Rank":  "128661",
        "Kills":  "0",
        "Score":  "0",
        "Total":  "128661",
        "Killer":  "Sw****UK",
        "Weapon":  "m4"
    },
    {
        "ID":  "7",
        "GameUserId":  "6408****79452",
        "Name":  "M********nner",
        "Rank":  "122523",
        "Kills":  "3500",
        "Score":  "2",
        "Total":  "126023",
        "Killer":  "Sw****UK",
        "Weapon":  "shotgun"
    },
    {
        "ID":  "8",
        "GameUserId":  "59060***2163",
        "Name":  "A***g",
        "Rank":  "117207",
        "Kills":  "0",
        "Score":  "0",
        "Total":  "117207",
        "Killer":  "Ap*******ift",
        "Weapon":  "ak"
    },
    {
        "ID":  "9",
        "GameUserId":  "831467****599",
        "Name":  "*********ngstar",
        "Rank":  "112517",
        "Kills":  "0",
        "Score":  "0",
        "Total":  "112517",
        "Killer":  "Ol***r***",
        "Weapon":  "shotgun"
    },
    {
        "ID":  "10",
        "GameUserId":  "34542****7961",
        "Name":  "********Really",
        "Rank":  "108322",
        "Kills":  "5000",
        "Score":  "3",
        "Total":  "113322",
        "Killer":  "[Toxic Gas]",
        "Weapon":  null
    },
    {
        "ID":  "11",
        "GameUserId":  "904****58750",
        "Name":  "******tch",
        "Rank":  "104528",
        "Kills":  "3500",
        "Score":  "2",
        "Total":  "108028",
        "Killer":  "Sw****UK",
        "Weapon":  "shotgun"
    }
]

可能不需要太多的按摩就能满足您的要求。我更喜欢将数据客观化,而不是作为平面文本进行抓取。该脚本首先将数据视为固定宽度的 CSV 文件。一旦客观化为 CSV 数据,它就会转换为 JSON。如果没有人提出更好的答案,也许您可​​以使用它作为构建您的项目的骨架。


作为替代方案,这里有一个 Batch + JScript 多语言方法,它比 PowerShell 方法更高效,尽管它更长。它不会遭受 TextFieldParser() 类所需的开销,而且 JScript 通常比 PowerShell 更快。作为奖励,它应该可以与早期版本的 Windows 一起使用,只要安装了 IE 版本 9 或更高版本(我认为是 Vista SP2?)。如果您更熟悉 JavaScript,您可能更喜欢这个。

@if (@CodeSection == @Batch) @then
@echo off & setlocal

set "file=test.txt"

cscript /nologo /e:JScript "%~f0" < "%file%"
exit /b

@end // end Batch / begin JScript

var stdin = WSH.CreateObject('Scripting.FileSystemObject').GetStandardStream(0),
    file = stdin.ReadAll(),
    htmlfile = WSH.CreateObject('htmlfile'),
    csvfields = {"ID": 7, "GameUserId": 21, "Name": 25, "Rank": 6, "Kills": 9,
        "Score": 4, "Total": 12, "Killer": 26, "Weapon": null};

String.prototype.clean = function() {
    var val = this.replace(/[\(\)\.\+=]/g, '').replace(/^\s+|\s+$/g, '');
    return /^\d+$/.test(val) ? val * 1 : val;
}

htmlfile.write('<meta http-equiv="x-ua-compatible" content="IE=9" />');
var JSON = htmlfile.parentWindow.JSON,
    lines = obj = new htmlfile.parentWindow.Array();
htmlfile.close();

lines = file.split(/\r?\n/g);
lines.splice(0,4);
for (var i=0; i<lines.length; i++) {
    for (var field in csvfields) {
        if (!obj[i]) obj.push({});
        obj[i][field] = csvfields[field] ?
            lines[i].substring(0, csvfields[field]).clean() : lines[i].clean();
        lines[i] = lines[i].substring(csvfields[field]);
    }
}

WSH.Echo(JSON.stringify(obj, null, '    '));

输出类似,只是整数值不带引号。

关于php - .txt 写入 .json 文件批处理或需要 php 文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45245070/

相关文章:

javascript - 什么是 DOM js 内存容量?

batch-file - 如何从CMake运行.bat文件?

php - 昨天和今天在mysql中的日期

php - 让我的网站更安全,我使用的 password_hash 是否正确?

javascript - 尽管返回了其他数据,但未设置 Ajax 成功变量

c - 如何指定批处理文件中的输入结束

powershell - 从 powershell 运行 Octave 脚本并等待其完成

php - 查找MySQL查询结果中某个字段的最大值

php - mysql中根据日期对获取的数据进行排序

jquery - 将 jQuery 与 Google Chrome 应用程序而不是 Chrome 扩展程序一起使用