skip to Main Content

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


  1. Try to use JsonArray instead of JsonObject.
    You can try to read string and then convert it

    String json = IOUtils.toString(ins, encoding);
    JsonArray jsonArray = new JsonArray(json);
    
    Login or Signup to reply.
  2. You can use ObjectMapper as alternative:

        ObjectMapper mapper = new ObjectMapper();
        JsonNode node = mapper.readTree(json);
        System.out.print(node.get(0).get("countryId").asText()); //getting countryId of the first object in array
    
    Login or Signup to reply.
  3. you can use following code to parse it

    Stri

    ng url = “http://qs.gls-one.de/api/countries/origin/configurations“;

    CloseableHttpClient client = HttpClientBuilder.create().build();
    HttpGet post = new HttpGet(url);
    HttpResponse response = client.execute(post);
    System.out.println("Response Code : "
                    + response.getStatusLine().getStatusCode());
    
    String response2= EntityUtils.toString(response.getEntity(), "UTF-8");
    JSONArray jsonArray = new JSONArray(response2);
    System.out.println(jsonArray);
    

    }

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search