Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/382.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 如何修复com.google.gson.stream.MalformedJsonException_Java_Json - Fatal编程技术网

Java 如何修复com.google.gson.stream.MalformedJsonException

Java 如何修复com.google.gson.stream.MalformedJsonException,java,json,Java,Json,我正在尝试从外部URL读取文件。该文件包含JSON格式的数据。我正在使用以下代码从文件中读取数据: URL url = new URL(url); URLConnection request = url.openConnection(); request.connect();JsonParser jp = new JsonParser(); JsonElement root = jp.parse(new InputStreamReader((InputStream) request.getCon

我正在尝试从外部URL读取文件。该文件包含JSON格式的数据。我正在使用以下代码从文件中读取数据:

URL url = new URL(url);
URLConnection request = url.openConnection();
request.connect();JsonParser jp = new JsonParser();
JsonElement root = jp.parse(new InputStreamReader((InputStream) request.getContent()));
JsonObject rootobj = root.getAsJsonObject();
JSONObject jsonObject = new JSONObject(rootobj.toString()); 
但我在尝试运行代码时遇到以下异常:

com.google.gson.JsonSyntaxException: com.google.gson.stream.MalformedJsonException: Use JsonReader.setLenient(true) to accept malformed JSON at line 2 column 2 
如何修复此错误?我现在花了一些时间在网上寻找s的解决方案,但没有任何帮助

我在下面添加示例数据:

{"address":"P.O. Box 939","city":"Indian River","country":"US","dateAdded":"2018-02-08T04:35:45Z","dateUpdated":"2018-02-08T04:35:45Z","descriptions":[{"dateSeen":["2018-02-01T21:34:00.000Z"],"sourceURLs":["https://www.weddingwire.com/biz/rental-express-indian-river/2274f039e0c45443.html"],"value":"Contact Rental Express in Indian River on WeddingWire. Browse Event Rentals prices, photos and 1 reviews, with a rating of 5.0 out of 5"}],"features":[{"key":"Average Rating","value":["5.0"]},{"key":"Event Items","value":["Chairs","Dance Floor","Lights","Photobooth","Tables","Tent Accessories","Tents"]},{"key":"Food/Beverage Items","value":["China","Flatware","Glassware","Linens"]}],"keys":["us/mi/indianriver/p.o.box939/-1646083636"],"latitude":"45.4125117","longitude":"-84.6125364","name":"Rental Express","phones":["2312389696"],"postalCode":"49749","province":"MI","sourceURLs":["https://www.weddingwire.com/biz/rental-express-indian-river/2274f039e0c45443.html"],"id":"AWFztIreIxWefVJwy6hK"}
{"address":"PO Box 6479","city":"Silver Spring","country":"US","dateAdded":"2018-02-08T04:35:35Z","dateUpdated":"2018-02-08T04:35:35Z","descriptions":[{"dateSeen":["2018-02-01T06:27:00.000Z"],"sourceURLs":["https://www.weddingwire.com/biz/indian-spring-country-club-silver-spring/9c020a063ea0fc84.html"],"value":"Contact Indian Spring Country Club in Silver Spring on WeddingWire. Browse Venue prices, photos and 3 reviews, with a rating of 4.3 out of 5"}],"features":[{"key":"Average Rating","value":["4.3"]}],"keys":["us/md/silverspring/pobox6479/1573156840"],"name":"Indian Spring Country Club","phones":["3018716000"],"postalCode":"20916","province":"MD","sourceURLs":["https://www.weddingwire.com/biz/indian-spring-country-club-silver-spring/9c020a063ea0fc84.html"],"id":"AWFztWW0IxWefVJwy7PH"}

也许这会有帮助

JsonReader jr = new JsonReader(new InputStreamReader((InputStream) request.getContent()));
jr.setLenient(true); //like the expetion says to accept the malformed json
JsonElement root = jp.parse(jr);


也许这会有帮助

JsonReader jr = new JsonReader(new InputStreamReader((InputStream) request.getContent()));
jr.setLenient(true); //like the expetion says to accept the malformed json
JsonElement root = jp.parse(jr);


对于第一个问题,格式错误的json异常表示您的输入可能不是有效的json;我会使用一个在线json格式化程序/验证器来检查这一点,比如在使用setLenient(true)忽略之前

关于第二个问题:如何使用多个URL构建Json数组,这是一种方法:

    try {
        // Here I use github api as a sample service:
        URL url1 = new URL("https://api.github.com/users/n1");
        URL url2 = new URL("https://api.github.com/users/n2");

        List<URL> urlsList = new ArrayList<URL>();
        urlsList.add(url1);
        urlsList.add(url2);

        JsonArray jsonArr = new JsonArray();
        for (URL url : urlsList) {
            URLConnection request = url.openConnection();
            request.connect();
            JsonObject jsonObj = new JsonParser().parse(new InputStreamReader((InputStream) request.getContent())).getAsJsonObject();
            jsonArr.add(jsonObj);   
        }
        System.out.println(jsonArr.toString());
    } catch (Exception e) {
        // do something here...
    }

对于第一个问题,MalformedJsonException表示您的输入可能不是有效的json;我会使用一个在线json格式化程序/验证器来检查这一点,比如在使用setLenient(true)忽略之前

关于第二个问题:如何使用多个URL构建Json数组,这是一种方法:

    try {
        // Here I use github api as a sample service:
        URL url1 = new URL("https://api.github.com/users/n1");
        URL url2 = new URL("https://api.github.com/users/n2");

        List<URL> urlsList = new ArrayList<URL>();
        urlsList.add(url1);
        urlsList.add(url2);

        JsonArray jsonArr = new JsonArray();
        for (URL url : urlsList) {
            URLConnection request = url.openConnection();
            request.connect();
            JsonObject jsonObj = new JsonParser().parse(new InputStreamReader((InputStream) request.getContent())).getAsJsonObject();
            jsonArr.add(jsonObj);   
        }
        System.out.println(jsonArr.toString());
    } catch (Exception e) {
        // do something here...
    }


JSON实际上有效吗?检查:@Øyvind Hauge,我从一个外部URL获取数据,该URL应该为我提供一个有效的JSON数据。数据非常大。请尝试在实现中使用最小的JSON示例:它能工作吗?如果是,则原始JSON响应可能无效(根据RFC4627)。JSON对象应包装在数组中(因此出现“无效JSON(RFC4627)多个JSON根元素”错误)。在服务器端,将所有元素包装在一个JSON数组中:[{object},{object},{object},…],它应该是有效的。@Øyvind Hauge,如何对从远程文件中获取的数据执行此操作,而我在远程文件中没有访问权限。将是具有类似数据的文件JSON是否实际有效?检查:@Øyvind Hauge,我从一个外部URL获取数据,该URL应该为我提供一个有效的JSON数据。数据非常大。请尝试在实现中使用最小的JSON示例:它能工作吗?如果是,则原始JSON响应可能无效(根据RFC4627)。JSON对象应包装在数组中(因此出现“无效JSON(RFC4627)多个JSON根元素”错误)。在服务器端,将所有元素包装在一个JSON数组中:[{object},{object},{object},…],它应该是有效的。@Øyvind Hauge,如何对从远程文件中获取的数据执行此操作,而我在远程文件中没有访问权限。将是一个具有类似数据的文件谢谢,这很有效。我是否可以在一个循环中使用多个URL解析数据,并将所有内容添加到一个JSON数组中。这是可行的,但在您实际尝试使用JSON时,可能会导致进一步的问题,因为它可能不是您期望的格式。@Øyvind Hauge,这是可行的,但它给了我一个JSON对象。数据由多个JSON对象组成。我无法通过这种方式获取全部数据JOSN的web资源是否公开?如果是这样,你可以发布链接,我来看看@GeoThomas@Wufo,我接受这个答案,因为它解决了异常,这就是问题所在。谢谢,成功了。我是否可以在一个循环中使用多个URL解析数据,并将所有内容添加到一个JSON数组中。这是可行的,但在您实际尝试使用JSON时,可能会导致进一步的问题,因为它可能不是您期望的格式。@Øyvind Hauge,这是可行的,但它给了我一个JSON对象。数据由多个JSON对象组成。我无法通过这种方式获取全部数据JOSN的web资源是否公开?如果是这样,你可以发布链接,我来看看@GeoThomas@Wufo,我接受这个答案,因为它解决了异常,这就是问题所在。我从URL获取的数据有一个小问题。我已经在我的问题中张贴了样品。此外,我还必须使用setLenient(true),因为我无法控制数据。我从URL获取的数据存在一个小问题。我已经在我的问题中张贴了样品。我还必须使用setLenient(true),因为我无法控制数据。