有没有办法通过在 soapUI 上使用 groovy 脚本将 Json 文件转换为 XML?
Is there a way to convert Json file to XML by using groovy script on soapUI?
我需要将 Json 文件格式转换成 XML 格式,我看到很多网页甚至在 java 中都使用这个我可以看到有很多有能力的人,但我在 groovy.
上找不到方法
我有这样一个文件:
{
"glossary": {
"title": "example glossary",
"GlossDiv": {
"title": "S",
"GlossList": {
"GlossEntry": {
"ID": "SGML",
"SortAs": "SGML",
"GlossTerm": "Standard Generalized Markup Language",
"Acronym": "SGML",
"Abbrev": "ISO 8879:1986",
"GlossDef": {
"para": "A meta-markup language, used to create markup languages such as DocBook.",
"GlossSeeAlso": ["GML", "XML"]
},
"GlossSee": "markup"
}
}
}
}
}
而且我希望能够直接转换为 XML,因为我们需要使用它来动态创建操作请求。
提前谢谢大家
她是可以帮助你的link。
基于上述 link,这里是完整的 groovy 脚本。
import net.sf.json.JSON
import net.sf.json.JSONSerializer
import net.sf.json.xml.XMLSerializer
String str = '''{ "glossary": { "title": "example glossary", "GlossDiv": { "title": "S", "GlossList": { "GlossEntry": { "ID": "SGML", "SortAs": "SGML", "GlossTerm": "Standard Generalized Markup Language", "Acronym": "SGML", "Abbrev": "ISO 8879:1986", "GlossDef": { "para": "A meta-markup language, used to create markup languages such as DocBook.", "GlossSeeAlso": ["GML", "XML"] }, "GlossSee": "markup" } } } } }'''
JSON json = JSONSerializer.toJSON( str )
XMLSerializer xmlSerializer = new XMLSerializer()
xmlSerializer.setTypeHintsCompatibility( false )
String xml = xmlSerializer.write( json )
System.out.println(xml)
顺便说一句,您需要下载library并将其添加到类路径,在本例中将其复制到SOAPUI_HOME/bin/ext目录下。
underscore-java 库包含来自 Json 和 toXml 的静态方法。
@Grab('com.github.javadev:underscore:1.32')
import com.github.underscore.U
def json = '''
{
"glossary": {
"title": "example glossary",
"GlossDiv": {
"title": "S",
"GlossList": {
"GlossEntry": {
"ID": "SGML",
"SortAs": "SGML",
"GlossTerm": "Standard Generalized Markup Language",
"Acronym": "SGML",
"Abbrev": "ISO 8879:1986",
"GlossDef": {
"para": "A meta-markup language, used to create markup languages such as DocBook.",
"GlossSeeAlso": ["GML", "XML"]
},
"GlossSee": "markup"
}
}
}
}
}
'''
println U.toXml(U.fromJson(json))
输出:
<?xml version="1.0" encoding="UTF-8"?>
<glossary>
<title>example glossary</title>
<GlossDiv>
<title>S</title>
<GlossList>
<GlossEntry>
<ID>SGML</ID>
<SortAs>SGML</SortAs>
<GlossTerm>Standard Generalized Markup Language</GlossTerm>
<Acronym>SGML</Acronym>
<Abbrev>ISO 8879:1986</Abbrev>
<GlossDef>
<para>A meta-markup language, used to create markup languages such as DocBook.</para>
<GlossSeeAlso>
<element>GML</element>
<element>XML</element>
</GlossSeeAlso>
</GlossDef>
<GlossSee>markup</GlossSee>
</GlossEntry>
</GlossList>
</GlossDiv>
</glossary>
我需要将 Json 文件格式转换成 XML 格式,我看到很多网页甚至在 java 中都使用这个我可以看到有很多有能力的人,但我在 groovy.
上找不到方法我有这样一个文件:
{
"glossary": {
"title": "example glossary",
"GlossDiv": {
"title": "S",
"GlossList": {
"GlossEntry": {
"ID": "SGML",
"SortAs": "SGML",
"GlossTerm": "Standard Generalized Markup Language",
"Acronym": "SGML",
"Abbrev": "ISO 8879:1986",
"GlossDef": {
"para": "A meta-markup language, used to create markup languages such as DocBook.",
"GlossSeeAlso": ["GML", "XML"]
},
"GlossSee": "markup"
}
}
}
}
}
而且我希望能够直接转换为 XML,因为我们需要使用它来动态创建操作请求。
提前谢谢大家
她是可以帮助你的link。
基于上述 link,这里是完整的 groovy 脚本。
import net.sf.json.JSON
import net.sf.json.JSONSerializer
import net.sf.json.xml.XMLSerializer
String str = '''{ "glossary": { "title": "example glossary", "GlossDiv": { "title": "S", "GlossList": { "GlossEntry": { "ID": "SGML", "SortAs": "SGML", "GlossTerm": "Standard Generalized Markup Language", "Acronym": "SGML", "Abbrev": "ISO 8879:1986", "GlossDef": { "para": "A meta-markup language, used to create markup languages such as DocBook.", "GlossSeeAlso": ["GML", "XML"] }, "GlossSee": "markup" } } } } }'''
JSON json = JSONSerializer.toJSON( str )
XMLSerializer xmlSerializer = new XMLSerializer()
xmlSerializer.setTypeHintsCompatibility( false )
String xml = xmlSerializer.write( json )
System.out.println(xml)
顺便说一句,您需要下载library并将其添加到类路径,在本例中将其复制到SOAPUI_HOME/bin/ext目录下。
underscore-java 库包含来自 Json 和 toXml 的静态方法。
@Grab('com.github.javadev:underscore:1.32')
import com.github.underscore.U
def json = '''
{
"glossary": {
"title": "example glossary",
"GlossDiv": {
"title": "S",
"GlossList": {
"GlossEntry": {
"ID": "SGML",
"SortAs": "SGML",
"GlossTerm": "Standard Generalized Markup Language",
"Acronym": "SGML",
"Abbrev": "ISO 8879:1986",
"GlossDef": {
"para": "A meta-markup language, used to create markup languages such as DocBook.",
"GlossSeeAlso": ["GML", "XML"]
},
"GlossSee": "markup"
}
}
}
}
}
'''
println U.toXml(U.fromJson(json))
输出:
<?xml version="1.0" encoding="UTF-8"?>
<glossary>
<title>example glossary</title>
<GlossDiv>
<title>S</title>
<GlossList>
<GlossEntry>
<ID>SGML</ID>
<SortAs>SGML</SortAs>
<GlossTerm>Standard Generalized Markup Language</GlossTerm>
<Acronym>SGML</Acronym>
<Abbrev>ISO 8879:1986</Abbrev>
<GlossDef>
<para>A meta-markup language, used to create markup languages such as DocBook.</para>
<GlossSeeAlso>
<element>GML</element>
<element>XML</element>
</GlossSeeAlso>
</GlossDef>
<GlossSee>markup</GlossSee>
</GlossEntry>
</GlossList>
</GlossDiv>
</glossary>