Reputation: 31
I am trying to read in the couchDB
:
"coordinates": {
"type": "Point",
"coordinates": [
-117.166272,
32.714312
]
},
I'm using CouchDB4j
and normally I could read normal attributes with:
mydocument.containsKey("text")
But I can't use it for "coordinates", as it has multiples attributes (type and coordinates).
It displays:
"Exception in thread "main" java.lang.ClassCastException:
net.sf.json.JSONObject cannot be cast to java.lang.String"
How could I resolve it, reading the coordinates and getting both point and coordinates?
Upvotes: 2
Views: 92
Reputation: 24596
I have just tried using version 2.4 of json-lib and it worked for me. I used groovy as it was quicker to test with groovy:
// I had to manually download the jar using:
// curl http://central.maven.org/maven2/net/sf/json-lib/json-lib/2.4/json-lib-2.4-jdk15.jar > ~/.groovy/grapes/net.sf.json-lib/json-lib/jars/json-lib-2.4.jar
@Grapes(
@Grab(group='net.sf.json-lib', module='json-lib', version='2.4')
)
import net.sf.json.*
import net.sf.json.groovy.*
builder = new net.sf.json.groovy.JsonGroovyBuilder()
def s = '''{
"_id": "12345",
"coordinates": {
"type": "Point",
"coordinates": [
-117.166272,
32.714312
]
},
"foo": "bar"
}'''
def jsonSlurper = new JsonSlurper()
def doc = jsonSlurper.parseText(s)
assert doc.containsKey("coordinates") == true
assert doc.get("coordinates").toString() ==
'{"type":"Point","coordinates":[-117.166275,32.714314]}'
assert doc.get("coordinates")['coordinates'][0] == -117.166275
Upvotes: 1