如何读取完整的输出? [英] How to read the complete output?

查看:83
本文介绍了如何读取完整的输出?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我必须解析.我将检索到的url响应存储在缓冲区中,但是当我将接收到的缓冲区移动到字符串中时,输出会损坏,因此无法解析它.但是,当我打印变量line时,我看到收到了整个响应.这是某种形式的字符串缓冲区溢出吗?我该如何解析响应,因为它还包含包装函数,所以我认为我什至不能使用JSONReader!

I have to parse this. I am storing the retreived url response in a buffer.But when I move the received buffer to a string I get a broken output and so I am unable to parse it. However when I print the variable line, I see the entire response is received. Is it some sort of buffer overflow for string? How can I parse the response, as it also contains a wrapper function and so I dont think I can even use the JSONReader!

这是代码-

URL url = new URL("http://chartapi.finance.yahoo.com/instrument/1.0/FB/chartdata;type=quote;range=1d/json");
HttpURLConnection urlConnection = (HttpURLConnection) url.openConnection();
try {
     InputStream inputStream = new BufferedInputStream(urlConnection.getInputStream());
     StringBuffer buffer = new StringBuffer();
     if (inputStream == null) return;

     reader = new BufferedReader(new InputStreamReader(inputStream));

     String line;
     while ((line = reader.readLine()) != null) {
     //Log.d(TAG,line);
     buffer.append(line + "\n");
     }

     Log.d(TAG,buffer.length() +buffer.toString());
}

推荐答案

您的代码,在正确打印内容(奇怪)的同时,里面没有任何System.out.println,您传递了似乎 JSON格式的链接<您拥有的文件的开头中strong> BUT

Your code, without any System.out.println inside while is printing the content correctly (weird), link you passed seems JSON format BUT in the start of the file you have

finance_charts_json_callback(

finance_charts_json_callback(

这不是JSON字符串的一部分.

which is not a part of a JSON string.

为了获取正确的数据,一种快速的解决方法是忽略此字符和最后的括号:

In order to get correct data, a fast workaround is ignore this characters AND the final parenthesis:

String jsonString = buffer.substring(30, buffer.length() - 2);
JsonObject jobj = new Gson().fromJson(jsonString, JsonObject.class);

现在有了JSonObject,因此可以通过属性名称进行引用:

Now you have a JSonObject, so you can refer it by attribute names:

System.out.println(jobj.get("Timestamp").toString());
System.out.println(jobj.get("labels").toString());
System.out.println(jobj.get("ranges").toString());
System.out.println(jobj.get("series").toString());

输出(减少)

{"min":1468589400,"max":1468612800}
[1468591200,1468594800,1468598400,1468602000,1468605600,1468609200,1468612800]
{"close":{"min":116.6050,"max":118.1800},"high":{"min":116.6562,"max":118.2800},"low":{"min":116.5800,"max":118.1600},"open":{"min":116.6000,"max":118.1786},"volume":{"min":100,"max":1390500}}
[{"Timestamp":1468589424,"close":117.8742,"high":117.9000,"low":117.8050,"open":117.8300,"volume":1390500},{"Timestamp":1468589460,"close":117.8350,"high":117.9900,"low":117.8200,"open":117.8850,"volume":112100},{"Timestamp":1468589578,"close":117.7300,"high":117.8200,"low":117.7000,"open":117.8200,"volume":70600},{"Timestamp":1468589580,"close":117.9770,"high":117.9900,"low":117.7100,"open":117.7600,"volume":91000},{"Timestamp":1468589698,"close":118.0650,"high":118.1000,"low":117.9700,"open":117.9700,"volume":353000},{"Timestamp":1468589701,"close":118.0250,"high":118.0900,"low":117.9210,"open":118.0735,"volume":108400},{"Timestamp":1468589819,"close":118.0900,"high":118.0900,"low":117.9500,"open":118.0201,"volume":121400},{"Timestamp":1468589879,"close":118.1800,"high":118.2300,"low":118.0900,"open":118.0900,"volume":186000},{"Timestamp":1468589939,"close":118.1700,"high":118.1900,"low":118.0700,"open":118.1764,"volume":84400},{"Timestamp":1468589940,"close":118.1800,"high":118.2800,"low":118.1600,"open":118.1786,"volume":152900},{"Timestamp":1468590000,"close":118.1300,"high":118.2100,"low":118.1100,"open":118.1600,"volume":53800},{"Timestamp":1468590060,"close":118.1800,"high":118.1800,"low":118.0950,"open":118.1200,"volume":91800},{"Timestamp":1468590120,"close":118.1110,"high":118.2200,"low":118.0707,"open":118.1700,"volume":65700},{"Timestamp":1468590239,"close":118.1300,"high":118.1400,"low":118.0179,"open":118.1400,"volume":57800},{"Timestamp":1468590240,"close":118.0500,"high":118.1800,"low":118.0500,"open":118.1100,"volume":75000},{"Timestamp":1468590300,"close":118.1000,"high":118.1500,"low":118.0250,"open":118.0449,"volume":51600},{"Timestamp":1468590360,"close":117.9600,"high":118.1100,"low":117.9000,"open":118.1000,"volume":99200},{"Timestamp":1468590421,"close":117.8400,"high":117.9700,"low":117.8200,"open":117.9600,"volume":173800},{"Timestamp":1468590480,"close":117.8800,"high":117.8800,"low":117.8200,"open":117.8300,"volume":92700},{"Timestamp":1468590541,"close":117.7000,"high":117.8850,"low":117.6810,"open":117.8850,"volume":114600},{"Timestamp":1468590659,"close":117.6865,"high":117.7200,"low":117.6600,"open":117.6800,"volume":83200},{"Timestamp":1468590719,"close":117.6300,"high":117.6900,"low":117.4900,"open":117.6800,"volume":121500}, (more results!!!)

完整代码:

public static void main(String[] args) throws Exception {
    URL url = new URL("http://chartapi.finance.yahoo.com/instrument/1.0/FB/chartdata;type=quote;range=1d/json");
    HttpURLConnection urlConnection = (HttpURLConnection) url.openConnection();
    InputStream inputStream = new BufferedInputStream(urlConnection.getInputStream());
    StringBuffer buffer = new StringBuffer();
    if (inputStream == null)
        return;

    BufferedReader reader = new BufferedReader(new InputStreamReader(inputStream));
    String line;

    while ((line = reader.readLine()) != null) {
        buffer.append(line + "\n");
    }

    String jsonString = buffer.substring(30, buffer.length() - 2);
    JsonObject jobj = new Gson().fromJson(jsonString, JsonObject.class);
    System.out.println(jobj.get("Timestamp").toString());
    System.out.println(jobj.get("labels").toString());
    System.out.println(jobj.get("ranges").toString());
    System.out.println(jobj.get("series").toString());

}

这篇关于如何读取完整的输出?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆