如何将漂亮的JSON放入REST api

时间:2016-02-28 09:22:18

标签: json web-services rest grails

我正在尝试构建一个REST服务,该服务接受XML并将其转换为JSON并调用接受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服务,不知道我做错了什么。请建议我。

1 个答案:

答案 0 :(得分:5)

您应在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 
    }
 } 

希望它有所帮助!!!