Iterating over a JSON array in scala

1.6k views Asked by At

I'm using the JSON lib net.sf.json(http://json-lib.sourceforge.net/apidocs/net/sf/json/package-summary.html) in my scala code. Also, I'm using the specs BDD framework (http://code.google.com/p/specs/) for unit testing. In the doBefore block, i have the following code:

doBefore {
  iter = serversJSON.iterator()
}

serversJSON is a JSONArray object. Outside the doBefore block, I have declared the variables used as follows

var serversJSON:JSONArray = null
var iter:Iterator[JSONArray] = null

But on compilation I'm getting the following error.

error: type mismatch; found : java.util.Iterator[?0] where type ?0 required: java.util.Iterator[net.sf.json.JSONArray] iter = serversJSON.iterator()

I guess the way i have declared the iter object outside doBefore is incorrect. How to fix this?

Please Help Thank You.

1

There are 1 answers

0
Submonoid On BEST ANSWER

As indicated here, the JSON library's iterator method returns a raw Iterator, not an Iterator[JSONArray]. You'll want to declare it as follows:

var serversJSON:JSONArray = null
var iter:Iterator[_] = null