我正在使用Eclipse将json转换为Java中的字符串。但我一直得到:
com.google.gson.JsonSyntaxException: java.lang.IllegalStateException
每次我尝试运行程序时都会出现错误消息。 Eclipse 无法识别我的代码中的任何错误/错误。我做了一些研究,似乎我的json无效(我使用了JSONLint
)。
这是我的json:
String json =
"{"
+"'$type': 'Tfl.Api.Presentation.Entities.RoadCorridor, Tfl.Api.Presentation.Entities',"
+ "'id' : a1,"
+ "'displayName' : 'A1',"
+ "'statusSeverity' : 'Good',"
+ "'statusSeverityDescription' : 'No Exceptional Delays',"
+ "'bounds' : '[[-0.25616,51.5319],[-0.10234,51.6562]]',"
+ "'envelope' : '[[-0.25616,51.5319],[-0.25616,51.6562],[-0.10234,51.6562],[-0.10234,51.5319],[-0.25616,51.5319]]',"
+ "'url' : 'https://api-argon.tfl.gov.uk/Road/a1'"
+ "}";
请有人告诉我json有什么问题以及如何更改它以使其有效?我对java和json很新,很抱歉,如果我错过了我应该包含的任何细节。
答案 0 :(得分:3)
您使用单引号作为json标识符。使用双引号。
"\"$type\": \"Tfl.Api.Presentation.Entities.RoadCorridor, Tfl.Api.Presentation.Entities\"," + etc.
该错误是因为您的json代码格式错误,这些单引号虽然在Java中很方便,但对json无效。
答案 1 :(得分:3)
你会使用单引号而不是双引号来解决问题
{
"$type": "Tfl.Api.Presentation.Entities.RoadCorridor, Tfl.Api.Presentation.Entities",
"id": "a1",
"displayName": "A1",
"statusSeverity": "Good",
"statusSeverityDescription": "No Exceptional Delays",
"bounds": "[[-0.25616,51.5319],[-0.10234,51.6562]]",
"envelope": "[[-0.25616,51.5319],[-0.25616,51.6562],[-0.10234,51.6562],[-0.10234,51.5319],[-0.25616,51.5319]]",
"url": "https://api-argon.tfl.gov.uk/Road/a1"
}
这里你的json现在有效,作为参考,我发现jsonlint.com非常适合帮助调试无效的json
答案 2 :(得分:0)
您可能希望使用下面的更改。处理数组时存在问题。
{
'$type': {
'Tfl.Api.Presentation.Entities.RoadCorridor, Tfl.Api.Presentation.Entities'
},
'id': a1,
'displayName': 'A1',
'statusSeverity': 'Good',
'statusSeverityDescription': 'No Exceptional Delays',
'bounds': '[[-0.25616,51.5319],[-0.10234,51.6562]]',
'envelope': '[[-0.25616,51.5319],[-0.25616,51.6562],[-0.10234,51.6562],[-0.10234,51.5319],[-0.25616,51.5319]]',
'url': 'https://api-argon.tfl.gov.uk/Road/a1'
}