Json数据无法正确读取URL

时间:2017-08-25 09:50:11

标签: java json

我正在尝试从url中读取Json数据。在下面的json格式不能正确地从链接读取jsonObject。我已经实现了代码,但仍然出现错误。需要一些关于错误的帮助。请注意,我没有创建任何模型类来解析json数据,而是从URL直接调用,然后在控制台中打印该json数据。

Json数据:

[
    {
        "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"
        }
    }
]

已实施的代码:

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();
        }

    }

}

获取错误:

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 个答案:

答案 0 :(得分:1)

尝试使用JsonArray代替JsonObject。 您可以尝试读取字符串然后将其转换

String json = IOUtils.toString(ins, encoding);
JsonArray jsonArray = new JsonArray(json);

答案 1 :(得分:0)

您可以使用ObjectMapper替代:

    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

答案 2 :(得分:0)

您可以使用以下代码进行解析

S研

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

 protected Void doInBackground(Void... arg0) {
        HttpHandler sh = new HttpHandler();
        // Making a request to url and getting response
        String jsonStr = sh.makeServiceCall(url);

        Log.e(TAG, "Response from url: " + jsonStr);

        if (jsonStr != null) {
            try {
                JSONObject jsonObj = new JSONObject(jsonStr);
                JSONArray contacts = jsonObj.getJSONArray("articles");

                for (int i = 0; i < contacts.length(); i++) {
                    JSONObject c = contacts.getJSONObject(i);

                    String author = c.getString("author");
                    String title = c.getString("title");
                    String decpn = c.getString("description");
                    String date=c.getString("publishedAt");
                    String image=c.getString("urlToImage");


                    // tmp hash map for single contact
                    HashMap<String, String> news = new HashMap<>();

                    news.put("author", author);
                    news.put("title", title);
                    news.put("publishedAt" , date);
                    news.put("description", decpn);
                    news.put("urlToImage" , image);

                    newslist.add(news);

}