json eval给出错误

时间:2013-11-19 14:27:39

标签: javascript json

我正在使用eval来获取以下JSON但是语法错误:预期的']'消息。我没有得到我所缺少的东西。 我的javascript stmt是

eval('var jsonResponse = ('+response+')');  

响应包含以下内容:

{iserror:"false",employees:["employee":{"employeeNbr":"SAN1234509","emplType":"SAN","agencyNbr":"","producerNbr":"123456789","remiCode":"SA","agentRate":"SA","typeCode":"I","reasonTxt":"S3","rsnDescription":"null","ymdeff":"20130101","ymdend":"20130101","voidtxt":"V","brokerName":"429610583","brokerNpn":"429610583","ymdtrans":"null","opNbr":"null"},
                            "employee":{"employeeNbr":"SAN1234509","emplType":"SAN","agencyNbr":"xxxxx-C","producerNbr":"1234567890,"remiCode":"SA","agentRate":"SA","typeCode":"I","reasonTxt":"S3","rsnDescription":"null","ymdeff":"20130101","ymdend":"20130101","voidtxt":"","brokerName":"429610583","brokerNpn":"429610583","ymdtrans":"null","opNbr":"null"},
                            "employee":{"employeeNbr":"SAN1234509","emplType":"SAN","agencyNbr":"","producerNbr":"123456789","remiCode":"SA","agentRate":"SA","typeCode":"I","reasonTxt":"S3","rsnDescription":"null","ymdeff":"20130101","ymdend":"20130101","voidtxt":"V","brokerName":"429610583","brokerNpn":"429610583","ymdtrans":"null","opNbr":"null"}]}

1 个答案:

答案 0 :(得分:1)

首先,你声称它是JSON,但事实并非如此。例如,iserror周围的引号需要为JSON。

具体地,

{iserror:"false",employees:[...]}

应该是

{"iserror":"false","employees":[...]}

但是因为你实际上将字符串传递给JavaScript解析器(eval),它只需要是JavaScript,而不是JSON。它也不是有效的JavaScript,这就是你收到错误的原因。

你有

{
   iserror:"false",
   employees:[
      "employee":{...},
      "employee":{...},
      "employee":{...}
   ]
}

:之后的"employee"错误。也许你打算用

{
   iserror:"false",
   employees:[
      {...},
      {...},
      {...}
   ]
}

另外,你有

"producerNbr":"1234567890,"remiCode":"SA"

而不是

"producerNbr":"1234567890","remiCode":"SA"