简体   繁体   中英

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.

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/

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM