JSON Marshaller 指定要显示的属性
JSON Marshaller specify the attributes to display
我有一个 class 订户(父)和 Optionsdetail(子)。他们是一对多的关系。
class 订户 {
String picture
String versionn
String title
String subtitle
Integer guid
Integer saleprice
Integer msrp
Integer costprice
String saletype
String category
String storecategory
String brand
String condition
String shipwithin
String modelskucode
String state
String link
String image
String image2
String image3
String image4
String description
String publishdate
Integer active
Integer weight
Integer quantity
String shippingprice
String whopay
String shiptolocation
String shippingmethod
String paymentmethod
String gsttype
String optionsstatus
Options options
Optionsdetails optionsdetails
下面是class选项详情
class Optionsdetails {
String sku
String usersku
Integer price
Integer saleprice
Integer msrp
Integer costprice
Integer quantity
Integer warningqty
String image1
Integer status
static belongTo = [subscriber: Subscriber]
我只想渲染这些 class 中的特定属性,所以我在 Bootstrap 处编写了如下代码
class BootStrap {
def init = { servletContext ->
JSON.registerObjectMarshaller(Subscriber) {
def returnArray = [:]
returnArray['title'] = it.title
returnArray['Id'] = it.guid
returnArray['description'] = it.description
returnArray['saletype'] = it.saletype
returnArray['options'] = ["optionName1": it.options.optionName1, "optionDetail1": it.options.optionDetail1]
returnArray['optionsdetails'] = it.optionsdetails.list()
return returnArray
}
}
class 订户属性没问题,但我如何自定义要从 class 选项详细信息中查看属性的内容。例如,我不想显示它的 ID,我想安排显示的属性。下面是 JSON 的样子
[
{
"title": "Female bag 2017 spring and summer new ladies shoulder bag wild shell small bag handbag female bag Messenger bag",
"Id": 710,
"description": "<font style=\"vertical-align: inherit;\"><font style=\"vertical-align: inherit;\">loading description...</font></font>",
"saletype": "B",
"options": {
"optionName1": "Color",
"optionDetail1": "Navy blue,light Grey,Taro purple,Deep purple,Dark gray,Pink,Light blue,Dark pink,black,Red wine,"
},
"optionsdetails": [
{
"id": 1,
"costprice": null,
"image1": "710.0",
"msrp": null,
"price": null,
"quantity": 4561,
"saleprice": null,
"sku": "Navy blue",
"status": 1,
"usersku": null,
"warningqty": 0
},
{
"id": 2,
"costprice": null,
"image1": "710.0",
"msrp": null,
"price": null,
"quantity": 4331,
"saleprice": null,
"sku": "light Grey",
"status": 1,
"usersku": null,
"warningqty": 0
},
请多指教。谢谢
我刚刚发现我需要为如下所示的 Optionsdetails 创建一个单独的 registerObjectMarshaller 来自定义属性。
那解决了问题。
JSON.registerObjectMarshaller(Optionsdetails) {
def returnArray = [:]
returnArray['sku'] = it.sku
returnArray['usersku'] = it.usersku
returnArray['price'] = it.price
returnArray['saleprice'] = it.saleprice
return returnArray
}
我有一个 class 订户(父)和 Optionsdetail(子)。他们是一对多的关系。
class 订户 {
String picture
String versionn
String title
String subtitle
Integer guid
Integer saleprice
Integer msrp
Integer costprice
String saletype
String category
String storecategory
String brand
String condition
String shipwithin
String modelskucode
String state
String link
String image
String image2
String image3
String image4
String description
String publishdate
Integer active
Integer weight
Integer quantity
String shippingprice
String whopay
String shiptolocation
String shippingmethod
String paymentmethod
String gsttype
String optionsstatus
Options options
Optionsdetails optionsdetails
下面是class选项详情
class Optionsdetails {
String sku
String usersku
Integer price
Integer saleprice
Integer msrp
Integer costprice
Integer quantity
Integer warningqty
String image1
Integer status
static belongTo = [subscriber: Subscriber]
我只想渲染这些 class 中的特定属性,所以我在 Bootstrap 处编写了如下代码
class BootStrap {
def init = { servletContext ->
JSON.registerObjectMarshaller(Subscriber) {
def returnArray = [:]
returnArray['title'] = it.title
returnArray['Id'] = it.guid
returnArray['description'] = it.description
returnArray['saletype'] = it.saletype
returnArray['options'] = ["optionName1": it.options.optionName1, "optionDetail1": it.options.optionDetail1]
returnArray['optionsdetails'] = it.optionsdetails.list()
return returnArray
}
}
class 订户属性没问题,但我如何自定义要从 class 选项详细信息中查看属性的内容。例如,我不想显示它的 ID,我想安排显示的属性。下面是 JSON 的样子
[
{
"title": "Female bag 2017 spring and summer new ladies shoulder bag wild shell small bag handbag female bag Messenger bag",
"Id": 710,
"description": "<font style=\"vertical-align: inherit;\"><font style=\"vertical-align: inherit;\">loading description...</font></font>",
"saletype": "B",
"options": {
"optionName1": "Color",
"optionDetail1": "Navy blue,light Grey,Taro purple,Deep purple,Dark gray,Pink,Light blue,Dark pink,black,Red wine,"
},
"optionsdetails": [
{
"id": 1,
"costprice": null,
"image1": "710.0",
"msrp": null,
"price": null,
"quantity": 4561,
"saleprice": null,
"sku": "Navy blue",
"status": 1,
"usersku": null,
"warningqty": 0
},
{
"id": 2,
"costprice": null,
"image1": "710.0",
"msrp": null,
"price": null,
"quantity": 4331,
"saleprice": null,
"sku": "light Grey",
"status": 1,
"usersku": null,
"warningqty": 0
},
请多指教。谢谢
我刚刚发现我需要为如下所示的 Optionsdetails 创建一个单独的 registerObjectMarshaller 来自定义属性。 那解决了问题。
JSON.registerObjectMarshaller(Optionsdetails) {
def returnArray = [:]
returnArray['sku'] = it.sku
returnArray['usersku'] = it.usersku
returnArray['price'] = it.price
returnArray['saleprice'] = it.saleprice
return returnArray
}