为什么我的JSON看起来正确,但仍然无效? [英] Why is my JSON invalid even though it looks correct?

查看:222
本文介绍了为什么我的JSON看起来正确,但仍然无效?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经为此工作了一段时间,但我只是不明白为什么我的JSON无效...

I've been working on this for quite a while, and I just don't understand why my JSON is invalid...

JSONLint显示此错误

JSONLint is showing this error

    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."
    },

它说是的,只要很久"就失败了

It's failing where it says 'Yes, as long'

JSON是在ColdFusion中动态创建的.

JSON is created in ColdFusion dynamically.

<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>

推荐答案

(正如其他答案所指出的那样,问题在于未转义的换行符,它破坏了JSON.这就是避免使用DIY JSON,而应使用内置函数 SerializeJSON().)

Lucee 5.2.8.39 +

尝试对Application.cfc中与与JSON序列化相关的设置的新支持 .新的设置使您可以覆盖序列化查询对象时使用的奇异默认CF:

Try the new support for JSON serialization-related settings in the Application.cfc. The new settings let you override the bizarre default CF has for serializing query objects:

// 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 ),就可以生成一个看起来非常合理的字符串,如下所示:

Now you can skip all the query looping. Simply execute the query and call serializeJSON( yourQuery ), to generate a wonderfully sane looking string like this:

[
  {
    "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版本

对于早期版本,请执行 serializeJSON 将数组转换为格式正确的JSON字符串.

For earlier versions, do what @Barmar recommended. Build an array of structures. Then use serializeJSON to convert the array into a properly formatted JSON string.

可运行示例

   <cfset yourArray = []>

   <cfloop query="getFAQs">
      <cfset yourArray.append( { "tags" : getFAQs.tags
                               , "question" : getFAQs.question
                               , "answer": getFAQs.answer
                             } )>    
   </cfloop>

   <cfset jsonString = serializeJSON( yourArray )>

如何删除新行?

生成适当的" JSON字符串后,运行 replace()并将\n替换为空字符串.

After generating a "proper" JSON string, run a replace() and substitute \n with an empty string.

  <cfset jsonString  = replace(jsonString , "\n", "", "all")>

要永久删除它们,您必须首先找到将它们插入数据库的代码,然后在其中进行修改.另外,更新任何现有的数据库记录以删除"\ n".

To permanently remove them, you'll have to find the code inserting them into the database in the first place, and modify it there. Also, update any existing database records to remove the "\n".

这篇关于为什么我的JSON看起来正确,但仍然无效?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆