Quiero analizar la respuesta JSON manualmente en Kotlin. Como recibo una respuesta JSON compleja que tiene algunos campos comunes. Por ejemplo, estoy obteniendo una respuesta inferior. { status: "success/false" apiId: 6 message: "Error msg if any" . . . // Here comes some JSON with complex structure where some fields are . // missing/omitted. Sometime […]
He intentado muchas sugerencias de otras mismas preguntas pero no ayuda, ¿cómo puedo ejecutar esta testing con éxito? @Test fun isJsonCorrectPersonConvert() { val gson = GsonBuilder().create() val json = gson.toJson("[{\"Id\":2,\"Text\":\"Математика\"},{\"Id\":5,\"Text\":\"Физика\"}]") val results = gson.fromJson(json, Array<Person>::class.java) Assert.assertNotNull(results) Assert.assertNotNull(results[0].Id) Assert.assertNotNull(results[0].Text) System.out.println(results.toString()) } data class Person(val Id: Int,val Text:String)
Si estoy usando javascript (o TypeScript), puedo hacer lo siguiente (solo idea); object = JSON.parse(jsonString) Y puedo usarlo así, alert(object.property); Súper simple. Si estoy usando Java, necesito crear classs y analizarlo para usarlo. Entiendo. ¿Qué hay de Kotlin y Swift? Tienen types opcionales, entonces ¿por qué una sola línea, un análisis simple similar a Javascript […]
Soy nuevo en kotlin y decidí que la mejor forma de aprender algo es comenzar a usarlo (entonces, la pregunta se relaciona más con el lenguaje que con la biblioteca de klaxon). Intento entender el primer fragment de la página de git de klaxon ( https://github.com/cbeust/klaxon ). Aquí está: fun parse(name: String) : Any? { […]
Estoy tratando de usar esta biblioteca y compile group: 'com.fasterxml.jackson.module', name: 'jackson-module-kotlin', version: '2.8.5' Tengo estructura de classs como click , y código fun main(args: Array<String>) { test() } fun test(){ val mapper = jacksonObjectMapper() val entity2 = Entity2("test_entity2") val entity1 = Entity1("test_entity1") val rootEntity1 = Entity1("root_entity1") entity2.entity1 = rootEntity1 entity1.parent = rootEntity1 entity1.entity2 = […]
Tengo un problema con los objects incorrectos en las lists. Por ejemplo, tengo un model JSON: { "items": [ { "id": 1, "name": "Item1" }, { "id": 2, "name": "Item2" }, { "id": [], "name": "Item3" } ] } y dos POJO data class BadList(val items: List<BadItem>) data class BadItem(val id: Int, val name: String) […]
Intento seguir este tutorial de Spark para crear una API REST usando Kotlin , y estoy luchando por publicar datos en el cuerpo JSON Me gustaría publicar los datos en el cuerpo, así: curl -H "Content-Type: application/json" -X POST -d '{"token" : "eyJhbGciOiJIUzI1NiJ9.eyJpc3MiOiJodHRwOi8vdHJ1c3R5YXBwLmNvbS8iLCJzdWIiOiJ1c2Vycy8xMzAwODE5MzgwIiwiZXhwIjoxNDg5OTgwNTI2fQ.5ZJG9GyhG-OCXg0C510MBFs9EQHdE909s4hpNxnM6LU"}' http://localhost:4567/tokens Sin embargo, esto obtendría un valor nulo en mi file Kotlin, […]
Me gustaría pasar una class de datos de Kotlin a javascript y serializarlo como JSON: engine.eval("""function jsFunction(pojo) { print(pojo); JSON.stringify({ "field": pojo })}""") engine.invokeFunction("jsFunction", Pojo(a="a", b="b")) el resultado que me hubiera gustado sería un json con: { "field": { "a": "a", "b": "b" } } pero el resultado es {} lugar La function de print […]
Hola, estoy intentando extraer los datos de mi file JSON con Kotlin en el estudio de Android: Aquí está mi file JSON: val future = RequestFuture.newFuture < JSONObject > () val request = JsonObjectRequest("localhost", JSONObject(), future, future) val requestQueue = Volley.newRequestQueue(this) requestQueue.add(request) try { val array = JSONObject().getJSONArray("server_response") for (i in 0..array.length() – 1) { […]
Estoy tratando de analizar una url que contiene JSON cada vez que presiono un button: button.setOnClickListener { doAsync{ val result = URL("http://date.jsontest.com/").readText() val parser: Parser = Parser() val stringBuilder: StringBuilder = StringBuilder(result) val json: JsonObject = parser.parse(stringBuilder) as JsonObject val time = "Time : ${json.string("time")}" uiThread { textView.setText(time) }}} Pero me aparece un error: Servicios […]