尝试在 Java 中将 JSON 解析为字符串

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/17308498/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-11-01 01:38:58  来源:igfitidea点击:

Trying to parse JSON to String in Java

javajsonparsing

提问by Ravi Thapliyal

I'm trying to parse this stock info at:

我正在尝试在以下位置解析此股票信息:

http://www.google.com/finance/info?client=ig&q=csco

http://www.google.com/finance/info?client=ig&q=csco

that's in JSON format to a map, essentially following this tutorial I saw using the quick-json jar but it keeps giving me an exception and I can't figure out why. Here's the code, any help is greatly appreciated

这是地图的 JSON 格式,基本上按照本教程我看到使用 quick-json jar 但它一直给我一个例外,我不知道为什么。这是代码,非常感谢任何帮助

Tutorial link: https://code.google.com/p/quick-json/

教程链接:https: //code.google.com/p/quick-json/

public static void main(String args[]) throws IOException
{
    String value="";
    URL uri = new URL("http://www.google.com/finance/info?client=ig&q=csco");
    BufferedReader input = new BufferedReader(new InputStreamReader(uri.openStream(), "UTF-8"));
    while(input.readLine()!=null)
    {
        value+=input.readLine();
    }
    JsonParserFactory factory = JsonParserFactory.getInstance();
    JSONParser parse = factory.newJsonParser();
    Map jsonData =parse.parseJson(value);
    System.out.println((String)jsonData.get("e"));
}

Here's the exception I get:

这是我得到的例外:

Exception in thread "main" com.json.exceptions.JSONParsingException: @Key-Heirarchy::root[0]/   @Key::  COMMA or ] is expected. but found :...@Position::5
    at com.json.utils.JSONUtility.handleFailure(JSONUtility.java:124)
    at com.json.parsers.JSONParser.stringLiteralTemplate(JSONParser.java:574)
    at com.json.parsers.JSONParser.nonValidatingValueTemplate(JSONParser.java:698)
    at com.json.parsers.JSONParser.jsonArrayTemplate(JSONParser.java:454)
    at com.json.parsers.JSONParser.parseJson(JSONParser.java:170)
    at parser.Scratch.main(Scratch.java:27)

EDIT: I also tried Map jsonData =parse.parseJson(value.substring(3) to start at [ but it still gives me an error

编辑:我也试过 Map jsonData =parse.parseJson(value.substring(3) 从 [ 开始,但它仍然给我一个错误

回答by Ravi Thapliyal

In addition to removing the leading //fix your loop as well. Change

除了删除前导//修复你的循环之外。改变

while(input.readLine()!=null) // skipping odd lines
{
    value+=input.readLine(); // reading even lines
}

to

String line = null;
while((line = input.readLine()) !=null)
{
    value +=line;
}

or, better use a StringBuilderlike

或者,更好地使用StringBuilder喜欢

String line = null;
StringBuilder json = new StringBuilder();
while((line = input.readLine()) !=null)
{
    json.append(line);
}
value = json.substring(3); // removes the leading "// "

EDIT:
I'm not familiar with your JSON parser. With the org.json.Java parser you could do it this way.

编辑
我不熟悉你的 JSON 解析器。使用org.json。Java解析器你可以这样做。

JSONArray jsonRoot = new JSONArray(value);
JSONObject quote = jsonRoot.get(0);
System.out.println ("e = " + quote.getString("e"));

But, as a workaround you could strip the []from StringBuilderas

但是,作为一种解决方法,您可以[]StringBuilderas

// removes the leading "// [" and trailing "]"
value = json.substring(4, json.length() - 1);

回答by Wilker Iceri

This json is not a valid, have two "//".

此 json 无效,有两个“//”。

Use http://jsonlint.com/to validate this

使用http://jsonlint.com/来验证这一点

回答by Brendan Long

The response from that URL starts with //, which isn't valid JSON:

来自该 URL 的响应以 开头//,这不是有效的 JSON:

// [ { "id": "99624" ,"t" : "CSCO" ,"e" : "NASDAQ" ,"l" : "24.00" ,"l_cur" : "24.00" ,"s": "2" ,"ltt":"4:00PM EDT" ,"lt" : "Jun 25, 4:00PM EDT" ,"c" : "-0.05" ,"cp" : "-0.21" ,"ccol" : "chr" ,"el": "24.00" ,"el_cur": "24.00" ,"elt" : "Jun 25, 5:54PM EDT" ,"ec" : "0.00" ,"ecp" : "0.00" ,"eccol" : "chb" ,"div" : "0.17" ,"yld" : "2.83" } ]

// [ { "id": "99624" ,"t" : "CSCO" ,"e" : "NASDAQ" ,"l" : "24.00" ,"l_cur" : "24.00" ,"s": "2 ","ltt":"4:00PM EDT","lt":"6 月 25 日下午 4:00 EDT","c":"-0.05","cp":"-0.21","ccol":" chr","el":"24.00","el_cur":"24.00","elt":"美国东部时间 6 月 25 日下午 5:54","ec":"0.00","ecp":"0.00"," eccol”:“chb”,“div”:“0.17”,“yld”:“2.83”}]

According to thisand this, the Google Finance API is deprecated anyway, so you may want to find something else.

根据thisthis,Google Finance API 无论如何都已弃用,因此您可能想找到其他东西。

回答by Java Guru

Following blog has enough number of very good examples on quick-json parser

下面的博客有足够多的关于 quick-json 解析器的很好的例子

It has got other competitive parsers examples as well

它还有其他有竞争力的解析器示例

http://codesnippets4all.com/html/parsers/json/quick-json.htm

http://codesnippets4all.com/html/parsers/json/quick-json.htm

回答by wazy

Add this to your code:

将此添加到您的代码中:

    String line = null;
    while((line = input.readLine()) !=null)
    {
        value += line;
    }
    value = value.replace("// ", "");

You need to replace the //at the beginning to "clean" the JSON before you can parse it.

您需要先替换//开头的 以“清理”JSON,然后才能解析它。

回答by swetha

It seems you are using old quick-json parser version. Use the latest version for parsing

看来您使用的是旧的 quick-json 解析器版本。使用最新版本进行解析

quick-json-1.0.2.3.jar

快速json-1.0.2.3.jar

I could see that the json is coming as follows,

我可以看到 json 如下,

// [
{
"id": "99624"
,"t" : "CSCO"
,"e" : "NASDAQ"
,"l" : "25.41"
,"l_cur" : "25.41"
,"s": "2"
,"ltt":"3:59PM EDT"
,"lt" : "Jul 10, 3:59PM EDT"
,"c" : "+0.25"
,"cp" : "1.01"
,"ccol" : "chg"
,"el": "25.55"
,"el_cur": "25.55"
,"elt" : "Jul 10, 7:07PM EDT"
,"ec" : "+0.14"
,"ecp" : "0.55"
,"eccol" : "chg"
,"div" : "0.17"
,"yld" : "2.68"
}
]

This is not valid JSON, it should not be preceded by //

这不是有效的 JSON,它不应以 //

// [

remove //and just use from [till end of the json string

删除//并从[json 字符串的末尾开始使用

i was able to parse successfully the below json string without //

我能够成功解析下面的json字符串而没有 //

[
{
"id": "99624"
,"t" : "CSCO"
,"e" : "NASDAQ"
,"l" : "25.41"
,"l_cur" : "25.41"
,"s": "2"
,"ltt":"3:59PM EDT"
,"lt" : "Jul 10, 3:59PM EDT"
,"c" : "+0.25"
,"cp" : "1.01"
,"ccol" : "chg"
,"el": "25.55"
,"el_cur": "25.55"
,"elt" : "Jul 10, 7:07PM EDT"
,"ec" : "+0.14"
,"ecp" : "0.55"
,"eccol" : "chg"
,"div" : "0.17"
,"yld" : "2.68"
}
]

Below is the output i've got with version quick-json-1.0.2.3.jar

以下是我使用版本quick-json-1.0.2.3.jar获得的输出

{root=[{e=NASDAQ, c=+0.25, div=0.17, l=25.41, lt=Jul 10, 3:59PM EDT, ec=+0.14, ltt=3:59PM EDT, elt=Jul 10, 7:07PM EDT, id=99624, yld=2.68, el_cur=25.55, t=CSCO, cp=1.01, s=2, el=25.55, l_cur=25.41, eccol=chg, ccol=chg, ecp=0.55}]}