I am trying to read Json data from url. On the following json format is not reading correctly as jsonObject from the link. I have code implemented but still getting error. Need some help regarding the error. Note that, I am not creating any model class to parse json data rather I am calling direct from URL and then print that json data in the console.
Json data :
[
{
"countryId": "BE",
"vat": 0.21,
"maxShortestEdge": 60,
"maxMiddleEdge": 80,
"maxLongestEdge": 200,
"maxLengthGirth": 300,
"maxWeight": 40,
"languages": [
"fr",
"nl",
"en"
],
"defaultLanguage": "nl",
"currency": {
"id": "EUR",
"messageId": "general.units.currency.eur"
}
},
{
"countryId": "DE",
"vat": 0.19,
"maxShortestEdge": 60,
"maxMiddleEdge": 80,
"maxLongestEdge": 200,
"maxLengthGirth": 300,
"maxWeight": 40,
"languages": [
"de",
"en"
],
"defaultLanguage": "de",
"currency": {
"id": "EUR",
"messageId": "general.units.currency.eur"
},
"tracking": {
"google": "UA-64686897-5",
"facebook": "591925420983129"
}
},
{
"countryId": "LU",
"vat": 0.17,
"maxShortestEdge": 60,
"maxMiddleEdge": 80,
"maxLongestEdge": 200,
"maxLengthGirth": 300,
"maxWeight": 40,
"languages": [
"fr",
"en"
],
"defaultLanguage": "fr",
"currency": {
"id": "EUR",
"messageId": "general.units.currency.eur"
}
}
]
Code Implemented:
public class RestTest {
public static void main(String[] args) {
Proxy proxy = new Proxy(Proxy.Type.HTTP, new InetSocketAddress("proxy.xxxxx.com",8080));
try {
URL url = new URL("http://test.one.de/api/");
HttpURLConnection http = (HttpURLConnection)url.openConnection(proxy);
InputStream ins = http.getInputStream();
JsonParser jsonParser = new JsonParser();
JsonObject jsonObject = (JsonObject)jsonParser.parse(new InputStreamReader(ins));
System.out.println(jsonObject);
}
catch (MalformedURLException e)
{
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
}
Error getting :
Exception in thread "main" com.google.gson.JsonSyntaxException: com.google.gson.stream.MalformedJsonException: Use JsonReader.setLenient(true) to accept malformed JSON at line 1 column 12 path $
at com.google.gson.JsonParser.parse(JsonParser.java:65)
at com.test.api.RestTest.main(RestTest.java:113)
Caused by: com.google.gson.stream.MalformedJsonException: Use JsonReader.setLenient(true) to accept malformed JSON at line 1 column 12 path $
at com.google.gson.stream.JsonReader.syntaxError(JsonReader.java:1567)
at com.google.gson.stream.JsonReader.checkLenient(JsonReader.java:1416)
at com.google.gson.stream.JsonReader.doPeek(JsonReader.java:546)
at com.google.gson.stream.JsonReader.peek(JsonReader.java:429)
at com.google.gson.JsonParser.parse(JsonParser.java:60)
... 1 more
3
Answers
Try to use
JsonArray
instead ofJsonObject
.You can try to read string and then convert it
You can use
ObjectMapper
as alternative:you can use following code to parse it
Stri
ng url = “http://qs.gls-one.de/api/countries/origin/configurations“;
}