ftl将多行转换为单行,将其解析为json对象

时间:2015-05-22 06:29:26

标签: javascript json

我已经写了ftl文件,我正在获取产品信息。 产品信息如下:

<!-- language: lang-html -->
{
   "productName": "jjj",
   "productId": "10021",
   "description": "this is line
                   for testing
                   It is too long
                   so i am not
                   able to make 
                   it as a single
                   line
                   can anyone plase
                   help me
                   for this. i ma not able to pasre it as josn object",
  "price": "111",
  "priceTypeId": "1000"
}

当我尝试使用parseJSON()方法将此产品信息解析为JSON对象时。这是一个例外 现在我想从上面的内容描述到一行,其中新行应该用\ n \ r \ n替换,所以我可以用json对象解析它。 我试了一件事:

<!-- language: lang-html -->
<#assign desc = desc?replace("(\r\n)+", "</p><p>",'r')>

它将desc内容放在一行中。但我想在多行显示desc内容。我可以用什么方法?有谁能够帮我?最后我必须将这个desc解析为json对象。提前谢谢。

我的ftl文件:

<div id="jsonObj">
{
  <#if product?has_content  >
      <#if product.productName?if_exists?contains('&quot;')>
      <#--  condition to allow " in product name-->
      <#assign name=product.productName?replace('&quot;', '\\&quot;')/>
        "productName": "${name?if_exists}",
      <#else>
      "productName": "${product.productName?if_exists}",
      </#if>
      "productId": "${product.productId?if_exists}",
       <#--  condition to allow " and \ in product description-->
       <#if product.description?if_exists?contains('&quot;') || product.description?if_exists?contains('&#92;')>
         <#assign desc=product.description?if_exists>
         <#assign desc=desc?replace('&#92;', '&#92;&#92;')>
         <#assign desc=desc?replace('&quot;', '\\&quot;')>
          "description": "${desc?if_exists}",
        <#else>
        <#assign desc=product.description?if_exists>
         "description": "${desc?if_exists}",
        </#if>
      "price": "${product.sellingPrice?if_exists}",
      <#--  condition to allow " in part name-->
      <#if product.partNumber?if_exists?contains('&quot;')>
      <#assign pnumber=product.partNumber?replace('&quot;', '\\&quot;')/>
        "partNumber": "${pnumber?if_exists}",
      <#else>
      "partNumber": "${product.partNumber?if_exists}",
      </#if>
      "productCategoryId": "${product.productCategoryId?if_exists}",
      <#assign count=1/>
      <#list productAdjustmentInfo as productAdjustment>
       "itemTax${productAdjustment.adjustmentId?if_exists}Value": "${productAdjustment.value?if_exists}",
       "itemTax${productAdjustment.adjustmentId?if_exists}Name":"${productAdjustment.name?if_exists}",
       "itemTax${productAdjustment.adjustmentId?if_exists}TemplateAdjustmentId":"${productAdjustment.templateAdjustmentId?if_exists}",
        <#assign count=count+1/>
      </#list>
      "priceTypeId": "${product.priceTypeId?if_exists}"
      <#else>
       "description": "",
       "price": ""
  </#if>
 }
</div>

在Javascript上,我使用下面的代码来解析

  var result = $(response.responseMessage + '#jsonObj').text();
  jsonObj = $.parseJSON(result);

0 个答案:

没有答案