3

I'm trying to parse this stock info at:

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

Tutorial link: 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

5
  • It tells you in exception that part of the input did not have the expected separator. Commented Jun 25, 2013 at 22:19
  • yeah but it it's in the correct format from what i can tell so I don't know why it's telling it it's not Commented Jun 25, 2013 at 22:20
  • It looks like you are adding 1 line every 2 readLine() inside "value" during the while loop. Commented Jun 25, 2013 at 22:22
  • // is not parsable here Commented Jun 25, 2013 at 22:23
  • Use one of the several "Online JSON parser" web pages you can find with Google to validate your JSON. Commented Jun 25, 2013 at 22:24

6 Answers 6

4

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

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.

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

// removes the leading "// [" and trailing "]"
value = json.substring(4, json.length() - 1);
Sign up to request clarification or add additional context in comments.

6 Comments

Skipping lines from input is not a problem?
Sorry, I totally looked over that, thanks for pointing that out, can't believe I still make that mistake now and then. It works now but it returns null. So it's not parsing correctly, any suggestions
Which JSON parser are you using?
quick-json. According to the debugger it's putting everything in "root". Am I forgetting some fundamental rule of hash tables?
Yes, the problem is your Map {} is within a JSON array []. You may need to explore the API some more to get two levels down into your json.
|
1

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

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

Comments

1

The response from that URL starts with //, which isn't valid 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" } ]

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

2 Comments

right, I also tried it with value.substring(3) to start it at the "[" but it still gives me an error
same one: Exception in thread "main" com.json.exceptions.JSONParsingException: "@"Key-Heirarchy::root[0]/ "@"Key:: COMMA or ] is expected. but found :...@Position::6
1

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

It has got other competitive parsers examples as well

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

1 Comment

Nice source . Try adding the relevant example in your answer here.
0

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.

3 Comments

@brendan long if you check the input the only occurence is at the beginning and I'm fairly positive it won't happen again.
If you look at once particular case of the input, then it seems to be safe, but it's still overkill, and unnecessarily increases the risk of bugs. It would be safer and more efficient to do: if(value.startswith("//")) value = value.substr(2);.
@brendan long yea I do agree with you. Just coded this for a specific question. I would edit it but I'm on the mobile device at the moment.
-1

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

quick-json-1.0.2.3.jar

I could see that the json is coming as follows,

// [
{
"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 //

// [

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

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

[
{
"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

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.