user2494770
user2494770

Reputation:

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

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

Upvotes: 3

Views: 12683

Answers (6)

Java Guru
Java Guru

Reputation: 11

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

Upvotes: 1

swetha
swetha

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

Upvotes: -1

Ravi K Thapliyal
Ravi K Thapliyal

Reputation: 51721

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);

Upvotes: 4

wazy
wazy

Reputation: 1065

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.

Upvotes: 0

Brendan Long
Brendan Long

Reputation: 54312

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.

Upvotes: 1

Wilker Iceri
Wilker Iceri

Reputation: 487

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

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

Upvotes: 1

Related Questions