2

I am trying to read a text file with JSON data in it using Java.

I use the following lines of code:

InputStream is = new FileInputStream(fileName);
JSONObject ret; 
try {
    s = IOUtils.toString(is);
    ret = (JSONObject)JSONSerializer.toJSON(s);
}

I however, am not able to get the JSON value in the ret variable and I in fact get a null value in the String 's'. Is there something that I am overlooking here?

I would greatly appreciate any help.

1
  • I am getting an exception at the " s = IOUtils.toString(ins);" line for some reason. I am not sure why? Is it not possible to use it this way, if the data in the file is JSON? Can some one please throw some light on this? Commented May 10, 2011 at 18:55

2 Answers 2

4

You may try out this example,

It worked well for me and can be extended easily to suit your json file http://answers.oreilly.com/topic/257-how-to-parse-json-in-java/

Sign up to request clarification or add additional context in comments.

Comments

2

and I in fact get a null value in the String 's'

Sounds like your file doesn't exist or is not readable. You can check this via File.exists() and File.canRead()

1 Comment

Brian, I did ensure that the file exists and it can be read. It satisfies both and I still get a null for the string.

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.