Tag: json

用android studio显示来自MySQL数据库的数据

我在一个项目上工作很简单,下面是我的代码(XML): <?xml version="1.0" encoding="utf-8"?> <android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:app="http://schemas.android.com/apk/res-auto" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" tools:context="com.test.test.MainActivity"> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Hello World!" android:id="@+id/tv" app:layout_constraintBottom_toBottomOf="parent" app:layout_constraintLeft_toLeftOf="parent" app:layout_constraintRight_toRightOf="parent" app:layout_constraintTop_toTopOf="parent" /> </android.support.constraint.ConstraintLayout> 这是我在Kotlin的代码: package com.test.test import android.os.AsyncTask import android.support.v7.app.AppCompatActivity import android.os.Bundle import android.view.View import kotlinx.android.synthetic.main.activity_main.* import org.json.JSONObject import java.io.BufferedReader import java.io.InputStream import java.io.InputStreamReader import java.net.HttpURLConnection import java.net.URL import java.util.zip.CheckedInputStream class MainActivity : AppCompatActivity() { […]

改造+ gson反序列化器:返回数组中

我有api返回json: {"countries":[{"id":1,"name":"Australia"},{"id":2,"name":"Austria"}, … ]} 我写模特班(Kotlin lang) data class Country(val id: Int, val name: String) 我想要使​​用retorift返回List <Models.Country>,从json中的“countries”字段请求 我下面写: interface DictService { @GET("/json/countries") public fun countries(): Observable<List<Models.Country>> companion object { fun create() : DictService { val gsonBuilder = GsonBuilder() val listType = object : TypeToken<List<Models.Country>>(){}.type gsonBuilder.registerTypeAdapter(listType, CountriesDeserializer) gsonBuilder.setFieldNamingPolicy(FieldNamingPolicy.LOWER_CASE_WITH_UNDERSCORES) val service = Retrofit.Builder() .baseUrl("…") .addCallAdapterFactory(RxJavaCallAdapterFactory.create()) .addConverterFactory(GsonConverterFactory.create(gsonBuilder.create())) .build() return […]

将JSON对象添加到现有的JSON文件

所以最近我的任务是创建一个实用程序,它将允许通过gson库轻松地将JSON添加到.json文件。 我已经在Kotlin中编写了这个代码: fun addObject(filePath: String, name: String, values: Array<Array<String>>) { try { var writer: JsonWriter = JsonWriter(FileWriter(File(filePath))) writer.beginObject() writer.name(name) writer.beginObject() for(item in values){ writer.name(item[0]).value(item[1]) } writer.endObject() writer.endObject() writer.close() println("[JSONUtil] Wrote object successfully!") } catch (e: IOException) { e.printStackTrace() } } 我使用了一个2维数组来允许用户在所述对象中添加具有任意数量值的不同对象。 例如,你会像这样运行它: addObject("C:\\Users\\Test\\Desktop\\JsonUtility\\output.json", "GENERAL", arrayOf(arrayOf("POS_X","2"), arrayOf("POS_Y","4"))) 这将创建以下JSON: {"GENERAL":{"POS_X":"2","POS_Y":"4"}} 这是它的目的和工作原理,我的问题是,再次运行该函数时,它会完全覆盖文件中的前一个JSON,这显然是不好的。 我的问题是: 我怎样才能在整个文件或特定点上添加新的JSON对象,例如“addObject(”GENERAL“,…)”? 我怎样才能使这个更好? 我对Kotlin相当陌生,而且大部分都是用Java编写的,所以Java解决方案很好,因为我相信我可以将其转换。 提前致谢! 编辑:新代码,不知道如何实现它: […]

处理过期的链接,同时从android应用程序中解析JSON

我正在设计一个解析json数组的Kotlin应用程序。 我已经成功完成了应用程序,但我被困在一个未成年人(在我的情况下主要)小故障。我已经成功地解析了数组,并在CardView显示所需的对象数组,并在卡片项被点击时打开浏览器中的链接。 问题 JSON包含某些已过期或不再使用的链接。 当用户点击特定的卡片 (带有任何华丽的信息或ImageView) 时,如何处理应用程序中的这种(过期的)链接 ? 我很喜欢Android的基础知识,并且仍然在学习如何在Android应用程序中设计Kotlin,并且尽我所能在互联网上搜索,但是我所有的努力都是一成不变的。 任何帮助受到热烈和良性的欢迎…. 下面是我的代码片段( FeedViewHolder.kt ): class FeedViewHolder(itemView: View):RecyclerView.ViewHolder(itemView), View.OnClickListener,View.OnLongClickListener { var txtTitle: TextView var txtPubdate: TextView var txtContent: TextView private var itemClickListener: ItemClickListener? = null init { txtTitle = itemView.findViewById(R.id.txtTitle) as TextView txtPubdate = itemView.findViewById(R.id.txtPubdate) as TextView txtContent = itemView.findViewById(R.id.txtContent) as TextView itemView.setOnClickListener(this) itemView.setOnLongClickListener(this) } fun setItemClickListener(itemClickListener: […]

类型不匹配:推断的类型是字符串? 但字符串预计在kotlin

我的主要活动中有以下代码: var qNa_list = parseQuestions(loadJSONFromAsset("qna_list.json")) fun loadJSONFromAsset(file_name:String): String? { var json: String? = null try { val isis = assets.open(file_name) val size = isis.available() val buffer = ByteArray(size) isis.read(buffer) isis.close() json = String(buffer, "UTF-8") } catch (ex: IOException) { ex.printStackTrace() return null } return json } 当我尝试编译它时,我得到以下错误。 我修复了一些由于可能的错误而导致的其他错误,但是这个错误是我无法解码的。 错误:(127,35)类型不匹配:推断的类型是字符串,但字符集是预期的 我已经改变了某些值为空来适应错误,但是json = String(buffer, "UTF-8") (UTF-8)总是用红色标出。

Kapt + LoganSquare不能在映射上工作

我正在使用Kotlin为Android构建我的应用程序。 然后我选择LoganSquare库从JSON – > Object进行解析。 经过一番努力,我得到了这个结果: @JsonObject class Line(@PrimaryKey @JsonField var id : Int, @JsonField var name : String) : Parcelable { companion object { @JsonIgnore @JvmField final val CREATOR: Parcelable.Creator<Line> = object : Parcelable.Creator<Line> { override fun createFromParcel(source: Parcel): Line = Line(source) override fun newArray(size: Int): Array<Line?> = arrayOfNulls(size) } } constructor(parcel : Parcel) […]

为什么我无法获取我的JSON文件的值?

您好,我正尝试在Android Studio中使用Kotlin提取我的JSON文件的数据: 这是我的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) { val objectYear = array.getJSONObject(i) val year = objectYear.getString("birthday") val response = future.get() // this will block textView2.setText(year.toString) // It […]

如何通过JSON.stringify在kotlin JS序列化一个地图到JSON字符串?

我的示例代码如下: fun main(args: Array<String>) { val testData = mapOf<String, Any>( "name" to "albert", "age" to 26, "work" to listOf("1", "2", "3") ) var value = JSON.stringify(testData, { _, value -> value.toString() }, 2) println(value) } 结果是"{name=albert, age=26, work=[1, 2, 3]}" 。 似乎错过了属性名称和字符串值的所有双引号。 我使用的是KotlinJS而不是Kotlin 那么,如何解决这个问题呢?

如何从Kotlin中的字符串创建JSONObject?

我需要将字符串{\"name\":\"test name\", \"age\":25}转换为JSONObject

在Kotlin中,FasterXML / jackson循环引用,@JsonIdentityInfo和UnresolvedForwardReference异常

我试图使用这个库和 compile group: 'com.fasterxml.jackson.module', name: 'jackson-module-kotlin', version: '2.8.5' 我有类结构这样的点击和代码 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 = entity2 rootEntity1.entity2 = entity2 val json = mapper.writeValueAsString(entity1) println(json) val result = mapper.readValue(json, Entity1::class.java) […]