Json-to-Json带着笑容

时间:2015-05-22 10:59:26

标签: json smooks

有没有办法在smooks中使用值替换来执行json-to-json?

在xml中执行此操作非常简单,例如

Xml文件:

<order id='332'>
<header>
    <customer number="123">Joe</customer>
</header>
<order-items>
    <order-item id='1'>
        <product>1</product>
        <quantity>2</quantity>
        <price>8.80</price>
    </order-item>
    <order-item id='2'>
        <product>2</product>
        <quantity>2</quantity>
        <price>8.80</price>
    </order-item>
    <order-item id='3'>
        <product>3</product>
        <quantity>2</quantity>
        <price>8.80</price>
    </order-item>
    <order-item id='4'>
        <product>4</product>
        <quantity>2</quantity>
        <price>8.80</price>
    </order-item>
    <order-item id='5'>
        <product>5</product>
        <quantity>2</quantity>
        <price>8.80</price>
    </order-item>
</order-items>

Smooks config:

<?xml version="1.0"?>
<smooks-resource-list xmlns="http://www.milyn.org/xsd/smooks-1.1.xsd"
                      xmlns:core="http://www.milyn.org/xsd/smooks/smooks-    core-1.3.xsd"
                      xmlns:ftl="http://www.milyn.org/xsd/smooks/freemarker-1.1.xsd">
    <resource-config selector="order-item">
        <resource>org.milyn.delivery.DomModelCreator</resource>
    </resource-config>

<ftl:freemarker applyOnElement="order-item/product">
    <ftl:template><!--<#t>
    <#t><#if .vars["order-item"].product?number gt 3><product>Car</product>
    <#t><#else><product>Bike</product></#if>--></ftl:template>
</ftl:freemarker>

</smooks-resource-list>

输出:

<order id="332">
<header>
    <customer number="123">Joe</customer>
</header>
<order-items>
    <order-item id="1">
        <product>Bike</product>
        <quantity>2</quantity>
        <price>8.80</price>
    </order-item>
    <order-item id="2">
        <product>Bike</product>
        <quantity>2</quantity>
        <price>8.80</price>
    </order-item>
    <order-item id="3">
        <product>Bike</product>
        <quantity>2</quantity>
        <price>8.80</price>
    </order-item>
    <order-item id="4">
        <product>Car</product>
        <quantity>2</quantity>
        <price>8.80</price>
    </order-item>
    <order-item id="5">
        <product>Car</product>
        <quantity>2</quantity>
        <price>8.80</price>
    </order-item>
</order-items>

完美。

现在,我想做一些与Json类似的事情,通过大多数线条不变,只修改一些值:

JSON:

{
"header": {
    "order id": "456",
    "date&time" : "Wed Nov 15 13:45:28 EST 2006",
    "customer": {
        "number": "123",
        "name": "Joe"
    }
},
"order item": [
    {
        "product id": "1",
        "quantity": "2",
        "price": "8.80"
    },
    {
        "product id": "2",
        "quantity": "2",
        "price": "8.80"
    },
    {
        "product id": "3",
        "quantity": "2",
        "price": "8.80"
    },
    {
        "product id": "4",
        "quantity": "2",
        "price": "8.80"
    },
    {
        "product id": "5",
        "quantity": "2",
        "price": "8.80"
    }
]}

Smooks config:

<?xml version="1.0"?>
<smooks-resource-list
     xmlns="http://www.milyn.org/xsd/smooks-1.1.xsd"
     xmlns:json="http://www.milyn.org/xsd/smooks/json-1.1.xsd"
     xmlns:core="http://www.milyn.org/xsd/smooks/smooks-core-1.3.xsd"
     xmlns:ftl="http://www.milyn.org/xsd/smooks/freemarker-1.1.xsd">

    <json:reader rootName="order" keyWhitspaceReplacement="_">
        <json:keyMap>
            <json:key from="date&amp;time" to="date_and_time" />
        </json:keyMap>
    </json:reader>

     <core:filterSettings type="SAX" defaultSerialization="true" />

    <resource-config selector="order_item/element">
       <resource>org.milyn.delivery.DomModelCreator</resource>
    </resource-config>

    <ftl:freemarker applyOnElement="order_item/element/product_id">
       <ftl:template><!--
       <#t><#if element.product_id?number gt 3>{"product_id":"Car"}
       <#t><#else>{"product_id":"Bike"}</#if>--></ftl:template>
    </ftl:freemarker>

</smooks-resource-list>

输出:

<order>
  <header>
    <order_id>456</order_id>
    <date_and_time>Wed Nov 15 13:45:28 EST 2006</date_and_time>
    <customer>
      <number>123</number>
      <name>Joe</name>
    </customer>
  </header>
  <order_item>
    <element>
      {"product_id":"Bike"}
      <quantity>2</quantity>
      <price>8.80</price>
    </element>
    <element>
      {"product_id":"Bike"}
      <quantity>2</quantity>
      <price>8.80</price>
    </element>
    <element>
      {"product_id":"Bike"}
      <quantity>2</quantity>
      <price>8.80</price>
    </element>
    <element>
       {"product_id":"Car"}
       <quantity>2</quantity>
       <price>8.80</price>
    </element>
    <element>
      {"product_id":"Car"}
      <quantity>2</quantity>
      <price>8.80</price>
      </element>
      </order_item>
</order>

我知道我可以关闭默认的血清,而这只会消除freemarker中的线条,但它并不是我想要的,即

{
  "header": {
    "order id": "456",
    "date&time" : "Wed Nov 15 13:45:28 EST 2006",
    "customer": {
      "number": "123",
      "name": "Joe"
    }
  },
  "order item": [
  {
    "product id": "Bike",
    "quantity": "2",
    "price": "8.80"
  },
  {
    "product id": "Bike",
    "quantity": "2",
    "price": "8.80"
  },
  {
    "product id": "Bike",
    "quantity": "2",
    "price": "8.80"
  },
  {
    "product id": "Car",
    "quantity": "2",
    "price": "8.80"
  },
  {
    "product id": "Car",
    "quantity": "2",
    "price": "8.80"
  }
]}

有没有办法将默认输出序列化切换为JSON,或者输出JSON的任何类似方式,而无需包含每个未更改的标记?

提前致谢。

0 个答案:

没有答案