Reputation: 20295
Using Scala 2.8 and Lift 2.2.
I'm calling the Github API and requesting repositories for a user. When the user has less than 30 repos one call is made and there is no need to concatenate JValues. However, when the user has more than 30 repos multiple calls are made. I would like to concatenate these results from these calls and then "flatten" them. i.e. The "repositories" name on a JValue should return all the repos not just the first 30.
The code below returns the following: Array(List(JObject(List(JField(repositories,JArray(...JObject(List(JField(repositories,JArray...))))))))
What I want is: Array(List(JObject(List(JField(repositories,JArray(....))))) where the repositories name points to all of the repos.
I've wrestled with this for a bit and can't seem to get 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 he has 30+ repos requires to calls to 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))
}
Upvotes: 1
Views: 927
Reputation: 2789
Function 'merge' should merge those JSONs like you described:
parse(line1) merge parse(line2)
Or more generically:
List(json1, json2, ...).foldLeft(JNothing: JValue)(_ merge _)
Upvotes: 4