开发者

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

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜