Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Trying to parse JSON to String in Java

Tags:

java

json

parsing

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


2 Answers

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);
like image 126
Ravi K Thapliyal Avatar answered May 17 '26 06:05

Ravi K Thapliyal


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

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

like image 42
Wilker Iceri Avatar answered May 17 '26 07:05

Wilker Iceri



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!