离线 · 无上传 · 浏览器本地完成Offline · no uploads · runs in the browser
TXT 转 JSON——离线、把文本里的 JSON 排好、不上传TXT to JSON offline — format JSON inside a text file
很多人手里的 JSON 就存成 `.txt`——从聊天工具、日志系统或旧编辑器里拿出来时,扩展名总是先丢。这一条走两步:TXT 先整段包进一个 `<pre>`(只转义 `&`、`<`、`>`,把换行写成 `<br>`,文档标题取文件名的主干(去掉扩展名),没有名字时是 `converted`),随后 HTML→JSON 发现文档里没有表格,就退一步把那个 `<pre>` 的文字当 JSON 解析,再用两级缩进重新写出。两个结果因此顺带得到:一份 minify 成一整行的数据变成可读的层级;类型是解析出来的类型——数字仍是数字、布尔仍是布尔、`null` 仍是 `null`,这与「从表格里取值」那条路相反,那里值一律是字符串。退一步也意味着边界清楚:**整段必须是合法 JSON**。带 `//` 注释、尾逗号或单引号的那一种会报解析错误,而不是把原文包成一个字符串塞进结果;普通文章、日志行同理。编码判定在第一步就做:先按 BOM 与 NUL 的奇偶形状认 UTF-16,再依次试严格的 UTF-8、GB18030、GBK,四种都不像文本就明确报错,不会产出乱码。全程在这个标签页里完成,只有 storage 权限,没有任何网络请求。A lot of JSON lives in files named `.txt` — the extension is the first thing lost when data comes out of a chat app, a log viewer or an old editor. This route is two steps: the text is wrapped whole into a `<pre>` (only `&`, `<` and `>` are escaped, line breaks become `<br>`, and the document title is the file’s basename with the extension dropped, or `converted` when there is none), then HTML to JSON, finding no table in the document, falls back to reading that `<pre>` as JSON and writes it out again indented two spaces. Two results come with it: data minified onto one line becomes a readable hierarchy, and the types are the parsed ones — numbers stay numbers, booleans stay booleans, `null` stays `null`, which is the opposite of the table route, where every value is a string. The same fallback marks the boundary clearly: **the whole text has to be valid JSON**. A file with `//` comments, trailing commas or single quotes reports a parse error instead of having its contents wrapped into one string, and so does ordinary prose or log lines. Encoding is settled in the first step: UTF-16 from its BOM or NUL parity, then strict UTF-8, GB18030 and GBK in order, with an explicit failure when the bytes are not text at all. All of it runs in this tab on the storage permission only, with no network request.
这条链路怎么走How the route runs
纯文本 (TXT)plain text (TXT) HTMLHTML JSONJSON
共 2 步,由扩展的路径搜索自动选出,中间产物不落盘。2 steps, chosen by the extension's own path search; intermediate results never touch the disk.
会保留什么What carries over
- 原文是 JSON 就按两级缩进重排:挤成一整行的 minify 数据,转完就是能读的层级JSON in, two-space indentation out: data minified onto one line comes back as a readable hierarchy
- 类型是解析出来的类型:数字、布尔与 `null` 保持原样,不会被统一收成字符串The types are the parsed ones — numbers, booleans and `null` stay themselves instead of collapsing into strings
- 嵌套原样保留:对象套数组、数组套对象都还在结果里,不会被摊平Nesting survives as it is: objects inside arrays, arrays inside objects, never flattened
- 编码先判形状:UTF-16(BOM 或 NUL 奇偶)、UTF-8、GB18030、GBK 逐条严格解,四个都不像文本就报错而不是产出乱码Encoding is settled by shape first: UTF-16 by BOM or NUL parity, then strict UTF-8, GB18030 and GBK, failing outright when the bytes are not text
如实说明的限制What it does not do
- 认的是「整段是 JSON」:文章、日志、以文字写成的表格都会停在解析错误,原文不会被包成一个字符串交给你What it reads is “the whole text is JSON”: essays, logs and tables written as words stop at a parse error, and the text is not handed back wrapped in a string
- 换行不带着走:TXT→HTML 把 ` ` 写成 `<br>`,取回的是 `textContent`——JSON 语法不依赖换行所以照常解析,但你自己排的空行与缩进不会原样出现Line breaks do not travel: TXT to HTML turns ` ` into `<br>` and the read-back is `textContent`, so JSON parses anyway, but the blank lines and indentation you typed are not reproduced
- 值里的换行只有写成 ` ` 那一种转义才留得住:JSON 字符串里裸的换行本来就不合法,这一层不替它补A newline inside a value survives only as the ` ` escape: a bare one was never valid JSON, and this layer does not patch it
- 重复键按 JSON 解析的既有语义只留最后一个(`{"a":1,"a":2}` 出来是 `2`),这一层不合并也不报错Duplicate keys follow the parser’s own rule and keep the last one (`{"a":1,"a":2}` yields `2`) — no merging, no warning
- 超长整数按 JavaScript 的 number 处理:20 位账号那一种在解析那一刻就可能变成科学计数,要保字符请走 CSV / Excel 那两条按值取数的路Huge integers go through JavaScript’s number: a 20-digit account can become exponential notation at parse time, so keep the characters by taking the CSV or Excel route instead
实操提示Working with it
- 转完只想看:JSON→HTML 能把这份数据渲染成可折叠的网页树,键与值各自着色To read it afterwards: JSON to HTML renders the data as a collapsible web tree with keyed colours
- 要表格:如果那段 JSON 是对象数组,走 TXT→CSV 或 TXT→Excel,键就是表头与列For a table: if that JSON is an array of objects, take TXT to CSV or TXT to Excel, where the keys become headers
- 文本本身不是 JSON:走 TXT→HTML / PDF / Word,它们保留等宽排版,不假装认得数据For text that is not JSON: use TXT to HTML, PDF or Word, which keep the monospace layout instead of pretending there is data
常见问题Questions people actually ask
我的 JSON 存成了 `.txt`,转出来和原文件一样吗?My JSON is saved as `.txt` — is the result the same file?
值一样,排版不一样。结果按两级缩进重新写出,所以原来手工排的空行、制表符对齐都不会保留;类型、顺序与嵌套都不变。要「一字不改」,那就不需要转,直接改扩展名即可。Same values, different layout. The result is rewritten with two-space indentation, so hand-typed blank lines and tab alignment do not survive, while types, order and nesting do. For bytes that must not change, renaming the file is the honest answer.
为什么我这份「看起来就是 JSON」却转失败了?It looks like JSON — why did it fail?
最常见的三种都不是合法 JSON:`//` 或 `/* */` 注释、最后一项后面的尾逗号、用单引号包字符串。`JSON.parse` 一律拒绝,这条路也就如实报错,不会猜你的意思、更不会把整段文字包成字符串装作成功。The three usual suspects are not valid JSON: `//` or `/* */` comments, the comma after the last item, and single-quoted strings. `JSON.parse` rejects all three, and this route reports that plainly instead of guessing what you meant or wrapping the text in a string to look like a success.
这一步会把文件发给谁吗?Does this step send the file anywhere?
不会。读的是文件自己的字节,两步都在扩展的标签页里,扩展只有 storage 权限、没有 host 权限;构建后的产物另有两道离线守卫逐条检查。No. The file’s own bytes are read, both steps run inside the extension’s tab, the manifest carries the storage permission and no host permission, and two offline guards audit the built artifact.
不需要账号,也不需要联网:装好扩展后点图标,在新标签页里打开工作台,文件从磁盘直接选。No account and no network: install the extension, click its icon, and the workbench opens in a new tab reading straight off your disk.
从 Chrome 应用商店安装Install from the Chrome Web Store 安装步骤Install steps GitHub 源码Source on GitHub