Reading multiple atributes CouchDB with Java

时间:2015-05-12 23:02:41

标签: java couchdb

I am trying to read in the $ sed '/^[[:space:]]*$/d' bl.txt Line 1 (next line has a tab) Line 2 (next has several space) Line 3 :

couchDB

I'm using "coordinates": { "type": "Point", "coordinates": [ -117.166272, 32.714312 ] }, and normally I could read normal attributes with:

CouchDB4j

But I can't use it for "coordinates", as it has multiples attributes (type and coordinates).

It displays:

mydocument.containsKey("text")

How could I resolve it, reading the coordinates and getting both point and coordinates?

1 个答案:

答案 0 :(得分:1)

我刚刚尝试使用json-lib的2.4版本,它对我有用。我使用了groovy,因为用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