reg_frenzy
reg_frenzy

Reputation: 81

Reading a text file with JSON data in Java

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.

Upvotes: 2

Views: 7552

Answers (2)

learner
learner

Reputation: 905

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/

Upvotes: 4

Brian Agnew
Brian Agnew

Reputation: 272237

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

Upvotes: 2

Related Questions