json - Working with concatenated JValues in liftjson -
using scala 2.8 , lift 2.2.
i'm calling github api , requesting repositories user. when user has less 30 repos 1 call made , there no need concatenate jvalues. however, when user has more 30 repos multiple calls made. concatenate these results these calls , "flatten" them. i.e. "repositories" name on jvalue should return repos not first 30.
the code below returns following: array(list(jobject(list(jfield(repositories,jarray(...jobject(list(jfield(repositories,jarray...))))))))
what want is: array(list(jobject(list(jfield(repositories,jarray(....))))) repositories name points all of repos.
i've wrestled bit , can't seem it.
import java.io._ import net.liftweb.json.jsonast._ import net.liftweb.json.jsonparser._ import org.apache.http.client.methods.httpget import org.apache.http.impl.client.{ defaulthttpclient } object github extends application { implicit val formats = net.liftweb.json.defaultformats val client = new defaulthttpclient() var repos = jarray(list[jvalue]()) //pick on mojombo since has 30+ repos requires calls api var method = new httpget("http://github.com/api/v2/json/repos/show/" + "mojombo" + "?page=1") var response = client.execute(method) var instream = response.getentity.getcontent(); var reader = new bufferedreader(new inputstreamreader(instream)) var line1 = reader.readline method = new httpget("http://github.com/api/v2/json/repos/show/" + "mojombo" + "?page=2") response = client.execute(method) instream = response.getentity.getcontent(); reader = new bufferedreader(new inputstreamreader(instream)) val line2 = reader.readline println(parse(line1) ++ parse(line2)) }
function 'merge' should merge jsons described:
parse(line1) merge parse(line2)
or more generically:
list(json1, json2, ...).foldleft(jnothing: jvalue)(_ merge _)
Comments
Post a Comment