Domanda

Utilizzo della Scala 2.8 e Lift 2.2.

Sto chiamando l'API GitHub e richiedendo repository per un utente.Quando l'utente ha meno di 30 reposti, viene effettuata una chiamata e non è necessario concatenare jvalues.Tuttavia, quando l'utente ha più di 30 reposti vengono effettuati più chiamate.Vorrei concatenare questi risultati da queste chiamate e poi "appiattire".I.e. Il nome "dei repository" su un jvalue dovrebbe restituire tutti i reposti non solo i primi 30.

Il codice seguente restituisce quanto segue: Array (Elenco (Jobject (Elenco (Jfield (Jfield (Repository, Jarray (... Jobject (Elenco (Jfield (Repository, Jarray ...)))))))))Igienico

Quello che voglio è: array (Elenco (Jobject (Jobject (Jobject (Jfield (Repository, Jarray (....)))) Dove i repository Nome punta a Tutto dei reposti. .

Ho lottato con questo per un po 'e non riesco a capirlo.

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))
}
.

È stato utile?

Soluzione

Function 'merge' should merge those JSONs like you described:

parse(line1) merge parse(line2)

Or more generically:

List(json1, json2, ...).foldLeft(JNothing: JValue)(_ merge _)
Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top