跳到主要内容

XML 转 JSON

UpBox优盒工具箱,免费在线 XML 转 JSON:粘贴一段 XML,实时得到结构清晰的 JSON,属性以 @ 前缀保留、文本归到 #text、同名同级元素自动合成数组,支持 0-8 空格缩进。

无需注册 · 数据不上传No sign-up · nothing uploaded

XML 输入XML input

缩进 2 空格

JSON 结果JSON result

先粘贴一段 XML

属性统一放在 @ 前缀下,文本放在 #text;同名同级元素自动合成数组;只有一个子元素时不加数组,读代码时注意这点;注释、声明、DOCTYPE 会被忽略

本工具所有运算在本地浏览器完成,你的文件、文本不会上传到服务器

工具介绍

对接老系统、读 RSS、看 SOAP 报文,拿到的常是 XML;要写代码时又希望它是 JSON,手敲一遍很容易漏标签。 Connecting to a legacy system, reading RSS or inspecting a SOAP payload usually hands you XML — and writing code is easier with JSON. Retyping it by hand loses tags.

贴上原文就出结构:属性有 @ 前缀,节点文字在 #text,重复的条目自动变数组,缩进还能拖到 0 变一行。 Paste the source and the structure appears: attributes carry an @ prefix, node text sits in #text, repeats become arrays, and the indent slider goes down to 0 for a single line.

  • 属性前缀 @@ for attributes
  • 同名合成数组Repeats to arrays
  • 实时转换Live conversion
  • 缩进可调Indent control

使用说明

  1. 粘贴 XML。Paste the XML.
  2. 看右侧 JSON。Read the JSON on the right.
  3. 按需调缩进。Adjust the indent.
  4. 复制给代码用。Copy it into your code.

常见问题

为什么要加 @ 前缀?Why the @ prefix?
JSON 里属性和子节点共用同一个键空间,加前缀才能区分「id 属性」和「id 子元素」,这也是多数转换器的惯例。In JSON attributes and children share one key space; the prefix separates an id attribute from an id child element, which is the usual convention.
只有一个子元素为什么不加数组?Why no array for a single child?
因为 XML 本身不区分「一个」和「一组」,强制数组会让绝大多数单值字段多一层括号。需要固定数组时请在后端做归一。XML does not distinguish one from many; forcing arrays would wrap almost every scalar field. Normalize on the backend when you need fixed arrays.
报错信息怎么看?How do I read the error?
会指出是标签没闭合、闭合不匹配还是压根没有元素;闭合不匹配时会写出两层标签名。It tells you whether a tag is unclosed, a closing tag does not match, or there is no element at all; mismatches print both tag names.
大文件能处理吗?Does it handle big files?
纯前端解析,几 MB 的报文没问题;上百 MB 建议用命令行工具,别在浏览器里硬扛。Parsing is all client side and a few megabytes is fine; for hundreds of megabytes use a command line tool.
广告位

此处为广告位,可放置你的推广素材

支持图片、文字或 HTML 代码,替换本区块内容即可投放

浏览其他分类