groovy.json.JsonException:期待'}'或','但得到当前的char

时间:2016-09-22 19:56:26

标签: json groovy

我正试图让一段代码为我工作而没有太多运气。所以我把代码分解为这个令我悲伤的小片段。

任何人都可以帮助确定这个错误发生在世界上的原因吗?

import groovy.json.JsonSlurper;

String index = '[{accessCode=d20in9t, createdAt=2016-09-22T18:27:47.904Z, id=22cbf7c2-5d4e-4afe-a385-ddefb6e6dcf0, licenseType=mobileAppLicensesWithDevice, name=Lead Retrieval - Device Rental & App license, owner={entityType=exhibitor, id=df04e5e4-b69a-4585-8a26-659a8f8ae15f}, readableId=npy5gcqnz8t, termId=630493a4-4a70-4f4f-afaf-31610c14c181, updatedAt=2016-09-22T18:27:47.904Z}, {accessCode=fqcg0w9, createdAt=2016-09-22T18:27:47.904Z, id=74270a86-dfe8-4b58-82e7-080dd57ce57a, licenseType=mobileAppLicensesWithDevice, name=Lead Retrieval - Device Rental & App license, owner={entityType=exhibitor, id=df04e5e4-b69a-4585-8a26-659a8f8ae15f}, readableId=3atchw5lhai, termId=630493a4-4a70-4f4f-afaf-31610c14c181, updatedAt=2016-09-22T18:27:47.904Z}, {accessCode=o6eg9dp, createdAt=2016-09-22T18:27:47.904Z, id=8cc4f312-dae4-4daf-99cb-d060165ec8e8, licenseType=mobileAppLicensesWithDevice, name=Lead Retrieval - Device Rental & App license, owner={entityType=exhibitor, id=df04e5e4-b69a-4585-8a26-659a8f8ae15f}, readableId=q9pqu7nm5oa, termId=630493a4-4a70-4f4f-afaf-31610c14c181, updatedAt=2016-09-22T18:27:47.904Z}]'

def slurper = new groovy.json.JsonSlurper().parseText(index)
slurper.each {
    println(it)
}

http://ideone.com/3RrxyX

这是我在脚本中运行时出现的错误...

groovy.json.JsonException: expecting '}' or ',' but got current char 'a' with an int value of 97
           The current character read is 'a' with an int value of 97
          expecting '}' or ',' but got current char 'a' with an int value of 97
          line number 1
          index number 2
          [{accessCode=uvrbjeg, createdAt=2016-09-22T19:53:27.971Z, id=0328fce8-832d-499d-a19a-571ce19ce117, licenseType=mobileAppLicensesWithDevice, name=Lead Retrieval - Device Rental & App license, owner={entityType=exhibitor, id=6c3b0b9f-fed0-4b62-a241-f42b896dc0ff}, readableId=5btnmmqe49m, termId=630493a4-4a70-4f4f-afaf-31610c14c181, updatedAt=2016-09-22T19:53:27.971Z}, {accessCode=8fzwy2p, createdAt=2016-09-22T19:53:27.971Z, id=33db29b4-0e0d-449f-9ecf-f126dd745c87, licenseType=mobileAppLicensesWithDevice, name=Lead Retrieval - Device Rental & App license, owner={entityType=exhibitor, id=6c3b0b9f-fed0-4b62-a241-f42b896dc0ff}, readableId=izs6wr742ea, termId=630493a4-4a70-4f4f-afaf-31610c14c181, updatedAt=2016-09-22T19:53:27.971Z}, {accessCode=d4hjfue, createdAt=2016-09-22T19:53:27.971Z, id=8d49d092-3f2f-4801-85ae-aebca5d507d4, licenseType=mobileAppLicensesWithDevice, name=Lead Retrieval - Device Rental & App license, owner={entityType=exhibitor, id=6c3b0b9f-fed0-4b62-a241-f42b896dc0ff}, readableId=ur1onrasbd7, termId=630493a4-4a70-4f4f-afaf-31610c14c181, updatedAt=2016-09-22T19:53:27.971Z}]

任何想法都可以帮助解决这个问题。

3 个答案:

答案 0 :(得分:2)

您可以更改字符串格式以更正JSON(带引号的键) 或者您可以更改JsonSlurper的解析器类型:

new JsonSlurper().setType(JsonParserType.LAX).parseText(index)

答案 1 :(得分:0)

Json键必须是双引号

您的所有值都是字符串,因此它们也需要双引号。

您还需要"key":"value"而不是=

String index = '[{"accessCode":"d20in9t", "createdAt":"2016-09-22T18:27:47.904Z", "id":"22cbf7c2-5d4e-4afe-a385-ddefb6e6dcf0", "licenseType":"mobileAppLicensesWithDevice", "name":"Lead Retrieval - Device Rental & App license", "owner":{"entityType":"exhibitor", "id":"df04e5e4-b69a-4585-8a26-659a8f8ae15f"}, "readableId":"npy5gcqnz8t", "termId":"630493a4-4a70-4f4f-afaf-31610c14c181", "updatedAt":"2016-09-22T18:27:47.904Z"}, {"accessCode:"fqcg0w9", "createdAt":"2016-09-22T18:27:47.904Z", "id":"74270a86-dfe8-4b58-82e7-080dd57ce57a", "licenseType":"mobileAppLicensesWithDevice", "name":"Lead Retrieval - Device Rental & App license", "owner":{"entityType":"exhibitor", "id":"df04e5e4-b69a-4585-8a26-659a8f8ae15f"}, "readableId":"3atchw5lhai", "termId":"630493a4-4a70-4f4f-afaf-31610c14c181", "updatedAt":"2016-09-22T18:27:47.904Z"}, {"accessCode":"o6eg9dp", "createdAt":"2016-09-22T18:27:47.904Z", "id":"8cc4f312-dae4-4daf-99cb-d060165ec8e8", "licenseType":"mobileAppLicensesWithDevice", "name":"Lead Retrieval - Device Rental & App license", "owner":{"entityType"="exhibitor", "id":"df04e5e4-b69a-4585-8a26-659a8f8ae15f"}, "readableId":"q9pqu7nm5oa", "termId":"630493a4-4a70-4f4f-afaf-31610c14c181", "updatedAt":"2016-09-22T18:27:47.904Z"}]'

答案 2 :(得分:0)

格式不正确的json

  • 我有一个python代码生成了格式错误的python代码,其中
"{"enabled": True}"
  • 问题在于该值不是有效的json代码。
    • “ True”值应带双引号。

Groovy解析器对此感到困惑,并由于上述错误而失败

解决方案

  • 确保所有json代码均有效