Javascript ftl将多行转换为单行,以便将其解析为json对象
我已经写了ftl文件,在那里我可以得到产品信息。 产品信息如下:Javascript ftl将多行转换为单行,以便将其解析为json对象,javascript,json,Javascript,Json,我已经写了ftl文件,在那里我可以得到产品信息。 产品信息如下: <!-- language: lang-html --> { "productName": "jjj", "productId": "10021", "description": "this is line for testing It is too long so i am not
<!-- 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"
}
如果你的问题是关于Javascript的,请不要用java标记它!好的,陀螺。下次我会处理这个。你从哪里得到
.ftl
文件?是免费的吗?你能为这个对象提供完整的模板吗?可能你需要使用函数或者是Grundy这是FreeMarker。
<!-- language: lang-html -->
<#assign desc = desc?replace("(\r\n)+", "</p><p>",'r')>
<div id="jsonObj">
{
<#if product?has_content >
<#if product.productName?if_exists?contains('"')>
<#-- condition to allow " in product name-->
<#assign name=product.productName?replace('"', '\\"')/>
"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('"') || product.description?if_exists?contains('\')>
<#assign desc=product.description?if_exists>
<#assign desc=desc?replace('\', '\\')>
<#assign desc=desc?replace('"', '\\"')>
"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('"')>
<#assign pnumber=product.partNumber?replace('"', '\\"')/>
"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>
var result = $(response.responseMessage + '#jsonObj').text();
jsonObj = $.parseJSON(result);