Reputation: 3
I'm using Eclipse to covert json to a string in Java. But I keep getting the:
com.google.gson.JsonSyntaxException: java.lang.IllegalStateException
error message each time I try to run my program. Eclipse doesn't recognize any errors/faults with my code. I've done some research and it seems that my json is not valid (I used JSONLint
).
This is my 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'"
+ "}";
Please can someone tell me what is wrong with the json and how I can change it so its valid? I'm quite new to java and json so sorry if I missed out any details that I should have included.
Upvotes: 0
Views: 1124
Reputation: 143
You will oftern run into problems using single quotes instead of double quotes
{
"$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"
}
here is your json now valid, for reference i find that jsonlint.com is fantastic at helping debug invalid json
Upvotes: 3
Reputation: 1461
You may want to use change it like below. There is a problem in handling of arrays.
{
'$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'
}
refer here - http://www.w3schools.com/js/js_json_intro.asp
Upvotes: 0
Reputation: 1800
You're using single quotes for your json identifiers. Use double-quotes.
"\"$type\": \"Tfl.Api.Presentation.Entities.RoadCorridor, Tfl.Api.Presentation.Entities\"," + etc.
The error is because your json code is malformed, those single quotes, while convenient in Java, are not valid for json.
Upvotes: 3