You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

能否使用Groovy将非标准类JSON格式转换为标准JSON格式?

Absolutely! You can convert this non-standard JSON-like string to valid JSON using Groovy, and there are a couple of straightforward approaches to do it. Here are two reliable methods:

Method 1: Regex-Based Replacement (Safer for Untrusted Input)

This approach uses regular expressions to directly transform the non-standard string into valid JSON without executing any dynamic code. It works perfectly for your specific input structure:

def input = '{ id=102, brand=Disha, book=[{ slr=EFTR, description=Grammer, data=TYR, rate=true, numberOfPages=345, maxAllowed=12, currentPage=345 }, { slr=EFRE, description=English, data=TYR, rate=true, numberOfPages=345, maxAllowed=12, currentPage=345 }] }'

// Step 1: Quote all string values (skip booleans and numbers)
def withQuotedStrings = input.replaceAll(/=(\b(?!true|false)\w+\b)([,}\]])/, '="$1"$2')

// Step 2: Wrap all keys in double quotes
def withQuotedKeys = withQuotedStrings.replaceAll(/(\w+)=/, '"$1": ')

// Step 3: Convert specific numeric values to strings (id and maxAllowed)
def standardJson = withQuotedKeys.replaceAll(/"id":(\d+)/, '"id":"$1"')
                                 .replaceAll(/"maxAllowed":(\d+)/, '"maxAllowed":"$1"')

println standardJson

How it works:

  • Step 1: Quotes any value that's a word (letters/underscores) but not true or false (your string values like Disha, EFTR).
  • Step 2: Adds double quotes around all keys (like id → "id").
  • Step 3: Converts the numeric values of id and maxAllowed to strings while leaving other numbers (like numberOfPages) as numeric types.

Method 2: Groovy Map Parsing (More Flexible for Complex Structures)

If your input structure might vary or you need to manipulate the data further, you can convert the non-standard string into a Groovy map, adjust the types, then serialize it back to JSON:

import groovy.json.JsonBuilder

def input = '{ id=102, brand=Disha, book=[{ slr=EFTR, description=Grammer, data=TYR, rate=true, numberOfPages=345, maxAllowed=12, currentPage=345 }, { slr=EFRE, description=English, data=TYR, rate=true, numberOfPages=345, maxAllowed=12, currentPage=345 }] }'

// Convert to Groovy map literal syntax
def groovyLiteral = input.replaceAll(/=(\b(?!true|false)\w+\b)([,}\]])/, '="$1"$2')
                         .replaceAll(/=/, ':')
                         .replaceAll(/\{/, '[')
                         .replaceAll(/\}/, ']')

// Parse into a Groovy map (note: Eval.me is safe only for trusted input)
def dataMap = Eval.me(groovyLiteral)

// Adjust data types as needed
dataMap.id = dataMap.id.toString()
dataMap.book.each { book ->
    book.maxAllowed = book.maxAllowed.toString()
}

// Serialize to standard JSON
def jsonBuilder = new JsonBuilder(dataMap)
def standardJson = jsonBuilder.toPrettyString()

println standardJson

Notes:

  • Eval.me executes the string as Groovy code, so only use this if you trust the input source.
  • This method gives you full control to modify the data structure or types before converting to JSON.

Both methods will produce the exact standard JSON format you requested.

内容的提问来源于stack exchange,提问作者avidCoder

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.15 06:57:22