gpt4 book ai didi

json - 如何将漂亮的 JSON 放入 REST api 中

转载 作者:行者123 更新时间:2023-12-01 16:02:56 25 4
gpt4 key购买 nike

我正在尝试构建一个接受 XML 并将其转换为 JSON 的 REST 服务,并调用接受 JSON 的外部服务并将我的将 JSON 写入其中。我可以不使用漂亮格式放置 json,但我想以漂亮格式放置 json。请建议如何做,下面是我的代码...

package com.mypackge

import grails.converters.JSON
import grails.rest.RestfulController
import grails.plugins.rest.client.RestBuilder

class RestCustomerController extends RestfulController {
/*
static responseFormats = ['json', 'xml']
RestCustomerController() {
super(Customer)
}
*/
def index() {
convertXmlToJson()
}

def myJson = ''

def convertXmlToJson() {
def xml = ''' <Customer>
<customerid>9999999999999</customerid>
<ssn>8888</ssn>
<taxid>8888</taxid>
<address>
<addressline1>Yamber Ln</addressline1>
<addressline1>8664 SE</addressline1>
<city>CCCCC</city>
<state>CC</state>
<zipcode>97679</zipcode>
</address>
<firstname>Scott</firstname>
<middlename></middlename>
<lastname>David</lastname>
<account>
<accountno>576-294738943</accountno>
<accounttype>Lease</accounttype>
<accountsubtype></accountsubtype>
<accountstatus>complete</accountstatus>
<firstname>Scott</firstname>
<middlename></middlename>
<lastname>David</lastname>
<businessname></businessname>
<billingsystem>yoiuhn</billingsystem>
<brand></brand>
<plantype></plantype>
<billingaddress>
<addressline1>Yamber Ln</addressline1>
<addressline1>8664 SE </addressline1>
<city>CCCCC</city>
<state>CC</state>
<zipcode>97679</zipcode>
</billingaddress>
<job>
<jobid>8276437463728</jobid>
<jobstatus>SUCCESS</jobstatus>
</job>
</account>
</Customer>
'''.stripMargin()

// Parse it
def parsed = new XmlParser().parseText( xml )

def myId = parsed.customerid.text()
// Deal with each node:
def handle
handle = { node ->
if( node instanceof String ) {
node
}
else {
[ (node.name()): node.collect( handle ) ]
}
}
// Convert it to a Map containing a List of Maps
def jsonObject = [ (parsed.name()): parsed.collect { node ->
[ (node.name()): node.collect( handle ) ]
} ]
def json = new groovy.json.JsonBuilder(jsonObject) //.toPrettyString()
// Check it's what we expected
def mmyresp
try{
mmyresp = putRequest(myId,json)
}catch(Exception e) {
mmyresp = 'Please Validate JSON ....'
}
}

def putRequest(String id, JSON myJson) {
String url = "http://foo.com/customer/external/"+id
def rest = new RestBuilder()
def resp = rest.put(url){
contentType "application/json"
json{
myJson
}
}
return resp
}

}

记录按以下格式添加...

{"Customer":[{"customerid":["9999999999999"]},{"ssn":["8888"]},
{"taxid":["8888"]},{"address":[{"addressline1":["Yamber Ln"]},
{"addressline1":["8664 SE"]},{"city":["CCCCC"]},{"state":["CC"]},{"zipcode":["97679"]}]},
{"firstname":["Scott"]},{"middlename":[]},{"lastname":["David"]},{"businessname":[]},
{"account":[{"accountno":["576-294738943"]},{"accounttype":["Lease"]},{"accountsubtype":[]},
{"accountstatus":["complete"]},{"firstname":["Scott"]},{"middlename":[]},{"lastname":["David"]},
{"businessname":[]},{"billingsystem":["yoiuhn"]},{"brand":[]},{"plantype":[]},
{"billingaddress":[{"addressline1":["Yamber Ln"]},{"addressline1":["8664 SE"]},
{"city":["CCCCC"]},{"state":["CC"]},{"zipcode":["97679"]}]},{"job":[{"jobid":["8276437463728"]},
,{"jobstatus":["SUCCESS"]}]}]}]}

但我希望以漂亮的格式插入它。我尝试了 .toPrettyString() 但在尝试放入 json 时出现了强制转换异常。我是第一次尝试 REST 服务,不确定我哪里做错了。请就此向我提出建议。

最佳答案

您应该在 Config.groovy 中设置以下字段。

grails.converters.default.pretty.print = true

这对于 xml 和 json 来说都会很漂亮地打印出来。

您可以选择仅将其设置为 xml 或 json,如下所示:

对于 json:

grails.converters.json.pretty.print = true

对于 xml

grails.converters.xml.pretty.print = true

Config.groovy 条目的示例是:

environments {
development {
grails.converters.json.pretty.print = true
}
}

希望对你有帮助!!!

关于json - 如何将漂亮的 JSON 放入 REST api 中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35680758/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com