简体   繁体   English

使用JAVA从URL进行JSON解码

[英]JSON Decoding from URL with JAVA

I'm using this piece of code to grab json from a url and decode the inputstream with UTF8 我正在使用这段代码从URL中获取json并使用UTF8解码输入流

    try {
        InputStream is;
            is = new URL(url).openStream();
      BufferedReader rd = new BufferedReader(new InputStreamReader(is, StandardCharsets.UTF_8));
      StringBuilder sb = new StringBuilder();
      int cp;
      while ((cp = rd.read()) != -1) {
        sb.append((char) cp);
        System.out.println(cp + "  " + ((char) cp));
      }
      JSONArray json = new JSONArray(sb.toString());
      return json;
    } catch (IOException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    } catch (JSONException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    } finally {
    }

When I run it on a website http://www.somewebsite.com/data.json I get a bunch of jibberish 当我在网站http://www.somewebsite.com/data.json上运行它时,我得到了一堆胡言乱语

Here are the first two chars that I get back (3 and 65533) 这是我回来的前两个字符(3和65533)

31 31
65533 \� 65533 \\ ufffd

I understand the 65533 is a replacement character so I'm doing something wrong with encoding. 我知道65533是替换字符,因此我在编码方面做错了。

I tried uploading this same json text to another website 我尝试将相同的json文本上传到另一个网站

http://ex . http:// ex www.someotherwebsite.com/abcd It decodes fine. www.someotherwebsite.com/abcd可以正常解码。

I tried downloading both files and they are both UTF8. 我尝试下载两个文件,它们都是UTF8。 Is it because one is a .json file and the other one is not? 是因为一个是.json文件,而另一个不是。

Try this method Charset.forName("UTF-8").encode(your-data-in-string) . 尝试使用此方法Charset.forName("UTF-8").encode(your-data-in-string) Or if this String is in a servlet and you want to send it as a response, use setCharacterEncoding("UTF-8") with the response object . 或者,如果此String在servlet中,并且您希望将其作为响应发送,则将setCharacterEncoding(“ UTF-8”)与response对象一起使用。

You are just converting the charset. 您只是在转换字符集。 If you are getting data from the url, you should check if the parameter is url encoded and decoded it before parsing the JSON string. 如果要从url获取数据,则应在解析JSON字符串之前检查参数是否经过url编码和解码。

If anyone is wondering, the json file was not utf-8. 如果有人想知道,json文件不是utf-8。 I was trying to decode using utf-8 when it was gzip encoded. 我尝试使用utf-8进行gzip编码时进行解码。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM