简体   繁体   English

尝试在Java中将JSON解析为String

[英]Trying to parse JSON to String in Java

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. 这是JSON格式的地图,基本上遵循本教程,我看到使用quick-json jar,但它一直给我一个例外,我无法弄清楚为什么。 Here's the code, any help is greatly appreciated 这是代码,非常感谢任何帮助

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)开始[但它仍然给我一个错误

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 StringBuilder like 或者,更好地使用像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. 我不熟悉你的JSON解析器。 With the org.json. 使用org.json。 Java parser you could do it this way. 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 StringBuilder as 但是,作为一种解决方法,您可以将[]StringBuilder剥离为

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

This json is not a valid, have two "//". 这个json不是有效的,有两个“//”。

Use http://jsonlint.com/ to validate this 使用http://jsonlint.com/验证这一点

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”:“纳斯达克”,“l”:“24.00”,“l_cur”:“24.00”,“s”:“2 “,”ltt“:”美国东部时间下午4:00“,”LT“:”美国东部时间6月25日下午4:00“,”c“:” - 0.05“,”cp“:” - - 0.21“,”ccol“:” chr“,”el“:”24.00“,”el_cur“:”24.00“,”elt“:”Jun 25,美国东部时间下午5:54“,”ec“:”0.00“,”ecp“:”0.00“,” eccol“:”chb“,”div“:”0.17“,”yld“:”2.83“}]

According to this and this , the Google Finance API is deprecated anyway, so you may want to find something else. 根据这一点 ,Google财务API无论如何都已弃用,因此您可能希望找到其他内容。

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

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,然后才能解析它。

It seems you are using old quick-json parser version. 看来你正在使用旧的quick-json解析器版本。 Use the latest version for parsing 使用最新版本进行解析

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}]}

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

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