我正在尝试解析我从第三方API收到的一些JSON。问题是一些JSON在引号内引用了引号,如下所示:
var json = '{"sentence": "My favorite sport is "dodgeball"." }';
我很确定浏览器会将报价解释为
"My favorite sport is"
然后在以下字符上遇到语法错误。我该如何避免这种情况?
编辑:我发布了问题的简化版本,但这里是实际的代码片段给我带来麻烦(问题是“酷供应商”包装引号):
{"content":"Vana Workforce is the Cloud HR Company. We are a global provider of human capital software and service solutions specifically designed to the needs of small and medium enterprises delivered on-demand.
Developed on the industry's leading cloud platform Force.com, Vana Human Capital Management delivers one complete and integrated human capital management solution for global organizations form hire to retire including the core human resource system of record, benefits & compensation, absence management, performance and talent management, career and succession planning, workforce collaboration, and workforce analytics.
Founded in 2008, Vana Workforce has offices in Canada and the United States with customers on every continent, and was recently recognized by Gartner Research as a "Cool Vendor" for 2010.","val":"Description"}
答案 0 :(得分:2)
这不是有效的JSON,因此您无法使用标准方法。
你要么必须逃避这样的引号:"sport is \" "
否则你需要编写自己的消毒剂
答案 1 :(得分:1)
这些报价应该被转义 -
"My favorite sport is \"dodgeball\"."
如果是真正的服务(不是家庭作业),这将是正确的。如果是家庭作业,那就去修复服务中的那个bug。