gpt4 book ai didi

java - 如何使用输入流读取严格可读的字符

转载 作者:太空宇宙 更新时间:2023-11-04 13:05:54 25 4
gpt4 key购买 nike

我在网页上回显了一个字符串。出于某种原因,我收到了很多添加的字符,或者有时没有输入完整的字符串。该字符串是:

        {"restaurant0": {
"name":"Jakes",
"deal_title":"Test",
"image":"Test",
"longitude":"36.067718",
"latitude":"-79.789334",
"county":"345 North Elm Street
Austin, TX 27401",
"description":"Test"

},"restaurant1": {
"name":"Jakes",
"deal_title":"Test",
"image":"Test",
"longitude":"36.067718",
"latitude":"-79.789334",
"county":"345 North Elm Street
Austin, TX 27401",
"description":"Test"

},"restaurant2": {
"name":"Jakes",
"deal_title":"Test",
"image":"Test",
"longitude":"36.067718",
"latitude":"-79.789334",
"county":"345 North Elm Street
Austin, TX 27401",
"description":"Test"

},"restaurant3": {
"name":"Jakes",
"deal_title":"Test",
"image":"Test",
"longitude":"36.067718",
"latitude":"-79.789334",
"county":"345 North Elm Street
Austin, TX 27401",
"description":"Test"

},"restaurant4": {
"name":"Jakes",
"deal_title":"Test",
"image":"Test",
"longitude":"36.067718",
"latitude":"-79.789334",
"county":"345 North Elm Street
Austin, TX 27401",
"description":"Test"

},"restaurant5": {
"name":" Jakes",
"deal_title":"Test",
"image":"Test",
"longitude":"36.067718", "latitude":"-79.789334",
"county":"345 North Elm Street Austin, TX 27401",
"description":"Test" },
"rows": {"row": "6"}}

我如何读取字符串的有趣部分在这里:

private String loadFromNetwork(String urlString) throws IOException {
InputStream stream = null;
String str = "";

try {
stream = downloadUrl(urlString);
str = readIt(stream, 65535);
} finally {
if (stream != null) {
stream.close();
}
}
return str;
}

并且

private String readIt(InputStream stream, int len) throws IOException, UnsupportedEncodingException {
Reader reader = null;
reader = new InputStreamReader(stream, "UTF-8");
char[] buffer = new char[len];
reader.read(buffer);
return new String(buffer);
}

但我有时会收到没有完整字符串的响应,这会导致我的代码无法正常工作:

        {"restaurant0": {
"name":"Jakes",
"deal_title":"Test",
"image":"Test",
"longitude":"36.067718",
"latitude":"-79.789334",
"county":"345 North Elm Street
Austin, TX 27401",
"description":"Test"

},"restaurant1": {
"name":"Jakes",
"deal_title":"Test",
"image":"Test",
"longitude":"36.067718",
"latitude":"-79.789334",
"county":"345 North Elm Street
Austin, TX 27401",
"description":"Test"

},"restaurant2": {
"name":"Jakes",
"deal_title":"Test",
"image":"Test",
"longitude":"36.067718",
"latitude":"-79.789334",
"county":"345 North Elm Street
Austin, TX 27401",
"description":"Test"

},"restaurant3": {
"name":"Jakes",
"deal_title":"Test",
"image":"Test",
"longitude":"36.067718",
"latitude":"-79.789334",
"county":"345 North Elm Street
Austin, TX 27401",
"description":"Test"

},"restaurant4": {
"name":"Jakes",
"deal_title":"Test",
"image":"Test",
"longitude":"36.067718",
"latitude":"-79.789334",
"county":"345 North Elm Street
Austin, TX 27401",
"description":"Test"

},"restaurant5": {
"name":" Jakes",
"deal_title":"Test",
"image":"Test", ���������������������������������������������...

最佳答案

你可以尝试这样的事情:

protected String getJSONFromInputStream(InputStream is)
{
if (is == null)
throw new NullPointerException();
//instantiates a reader with max size
BufferedReader reader = new BufferedReader(new InputStreamReader(is), 8 * 1024);

StringBuilder sb = new StringBuilder();

try
{
//reads the response line by line (and separates by a line-break)
String line;
while ((line = reader.readLine()) != null)
{
sb.append(line + "\n");
}
}
catch (IOException e)
{
e.printStackTrace();
}
finally
{
try
{
//closes the inputStream
is.close();
}
catch (IOException e)
{
e.printStackTrace();
}
}
return sb.toString();
}

然后通过实例化一个JSONArray来解析

为了快速引用,如果生成的字符串具有根前缀,只需启动一个 JSONObject:

JSONObject json = new JSONObject(jsonAsString);
JSONArray jArray = json.getJSONArray("rootTag");
for (int i = 0; i < jArray.length(); i++)
{
JSONObject currentJ = jArray.getJSONObject(i);
//Do Something with the object
}

关于java - 如何使用输入流读取严格可读的字符,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34444216/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com