我已经为此工作了一段时间,但我只是不明白为什么我的JSON无效...
JSONLint显示此错误
Error: Parse error on line 107:
...pair?", "answer": "Yes, as long as the
----------------------^
Expecting 'STRING', 'NUMBER', 'NULL', 'TRUE', 'FALSE', '{', '[', got 'undefined'
这是JSON的片段
{
"tags": "already transferred",
"question": "Can we transfer customers who have already been transferred previously? what is the dispo? warm transfer or already contacted?",
"answer": "Yes, mark as already contacted."
},
{
"tags": "secured debt",
"question": "If customer only has secured debts, can we still offer credit repair?",
"answer": "Yes, as long as they have at least $100 in secured/unsecured debt.
"},
{
"tags": "state",
"question": "Is the program state sensitive?",
"answer": "Yes, each partner has particular states that they service. The script engine will only offer services when the state is valid for partner who has that service."
},
说“是的,只要很久”就失败了
JSON是在ColdFusion中动态创建的。
<cfscript>faqCounter=1;</cfscript>
<CFLOOP query="getFAQs">
<cfoutput>
{"tags":"#getFAQs.tags#","question":"#getFAQs.question#","answer":"#getFAQs.answer#"}<cfif faqCounter<getFAQCount.getFAQPertinentCount>,</cfif>
</cfoutput>
<cfscript>faqCounter++;</cfscript>
</CFLOOP>
答案 0 :(得分:4)
答案 1 :(得分:1)
问题在于该字符串包含一个换行符作为文字,应为\n
。在大多数语言中,您都可以将数据过滤或序列化为JSON,它将为您处理这些转换。
此脚本利用serializeJSON()
函数将数据转换为JSON
<cfscript>
example = structnew();
example.firstname = "Yes";
example.lastname = "Man";
// changing the default serialization by specifying the type of "firstname" as string
metadata = {firstname: {type:"string"}};
example.setMetadata(metadata);
writeoutput(SerializeJSON(example));
</cfscript>
{"LASTNAME":"Man","FIRSTNAME":"Yes"}
答案 2 :(得分:1)
(正如其他答案已经指出的那样,问题在于未转义的新行破坏了JSON。这是避免DIY JSON的原因之一。相反,请使用内置函数{{3 }}。)
Lucee 5.2.8.39 +
尝试对SerializeJSON()的新支持。新的设置使您可以覆盖序列化查询对象时使用的奇异默认CF:
// serialize queries as an array of structures AND
// preserves the column name case used in the sql
this.serialization.preserveCaseForStructKey = true;
this.serialization.serializeQueryAs = "struct";
现在,您可以跳过所有查询循环。只需执行查询并调用serializeJSON( yourQuery )
,就可以生成一个看起来非常合理的字符串,如下所示:
[
{
"answer": "Yes, mark as already contacted.",
"tags": "already transferred",
"question": "Can we transfer customers who have already been transferred previously? what is the dispo? warm transfer or already contacted?"
},
{
"answer": "Yes, as long as they have at least $100 in secured/unsecured debt. ",
"tags": "secured debt",
"question": "If customer only has secured debts, can we still offer credit repair?"
}
]
早期的Lucee版本
对于早期版本,请执行JSON serialization-related settings in the Application.cfc。建立一系列结构。然后使用@Barmar recommended将数组转换为格式正确的JSON字符串。
<cfset yourArray = []>
<cfloop query="getFAQs">
<cfset yourArray.append( { "tags" : getFAQs.tags
, "question" : getFAQs.question
, "answer": getFAQs.answer
} )>
</cfloop>
<cfset jsonString = serializeJSON( yourArray )>
如何删除新行?
生成“适当的” JSON字符串后,运行Runnable Example并将\n
替换为空字符串。
<cfset jsonString = replace(jsonString , "\n", "", "all")>
要永久删除它们,您必须首先找到将它们插入数据库的代码,然后在其中进行修改。此外,更新任何现有的数据库记录以删除“ \ n”。