Tag: json

如何使用Android上的Kotlin从url解析JSON?

我一直试图从一个url解析JSON几个小时,但我太愚蠢了,我猜 我努力了 val result = URL(“http://date.jsontest.com/”).readText() 但它崩溃 java.net.MalformedURLException:无协议:date.jsontest.com 我试过了Klaxon的图书馆 fun parse(name: String) : Any? { val cls = Parser::class.java return cls.getResourceAsStream(name)?.let { inputStream -> return Parser().parse(inputStream) } } val obj = parse(“http://date.jsontest.com/”) as JsonObject 但它也崩溃 kotlin.TypeCastException:null不能转换为非nulltypescom.beust.klaxon.JsonObject 有人可以写在Kotlin最简单的方法来解析从这个链接的数据http://date.jsontest.com/

翻新2 POST XML并从API获取JSON答案。 与Kotlin

我需要发送XML到服务器,我想服务器发回我的JSON。 我正在使用Retrofit 2 + Kotlin Retrofit2方法(在方法接口中): @Headers(“Content-Type: application/xml; charset=urf-8”, “Accept: application/json”) @POST(Connectors.SECRET_LINK) fun sendCustomXml(@Body data: XmlHolder): Observable Retrofit2服务: private fun createService(serviceClass: Class): S { val retrofit = return } init { initLoggingInterceptor() val builder = Retrofit.Builder() .baseUrl(BuildConfig.API_URL) .addConverterFactory(GsonConverterFactory.create()) .addConverterFactory(SimpleXmlConverterFactory.create()) //here it is I thought .addCallAdapterFactory(RxJavaCallAdapterFactory.create()) mService = builder.client(getHttpClient()).build().create(ApiMethods::class.java)) } XML对象我需要发送像XML(标签等): @Root(name = “root_element”) class XmlHolder […]

使用Kotlin中的动态字段解析json对象

我有一个JSON对象: { “mobileNum”: “05x-xxxxxxx”, “appId”: “some_app”, “messageId”: “printUsersFirstTime”, “shouldSendDate”: “2017-10-03T16:20+03:00”, // this is optional “paramMap”: { “userName”: “some_name” }, “filters”: { “UNIQUE”: false, “RECENT”: “{ \”unit\”: \”MINUTE\”, \”size\”: 5 }” } 我使用Spring Boot和Kotlin。 Spring引导自动将提到的JSON映射到下面的模型bean: data class SmsDto( var mobileNum: String? = null, val appId: String? = null, val messageId: String? = null, var paramMap: Map? […]

如何在Kotlin中解析JSON?

我从服务中收到一个相当深的JSON对象字符串,我必须解析到一个JSON对象,然后将其映射到类。 如何将JSON字符串转换为Kotlin中的对象? 之后,映射到相应的类,我使用jackson的StdDeserializer。 问题出现在对象具有也必须被反序列化为类的属性的时刻。 我无法得到对象映射器,至少我不知道如何,在另一个反序列化器内。 在此先感谢您的帮助。 最好,本地,我试图减少我需要的依赖关系的数量,所以如果答案只是对JSON的操纵和分析它就够了。

如何在Kotlin中使用Gson注册一个InstanceCreator?

我可以使用代码1将MutableList保存到json字符串使用Gson正确,但是当我尝试从代码2从json字符串恢复MutableList对象时,我得到错误。我有搜索一些资源,似乎我需要注册一个InstanceCreator。 我怎样才能用Kotlin写一个InstanceCreator代码? 谢谢! 错误 Caused by: java.lang.RuntimeException: Unable to invoke no-args constructor for interface model.DeviceDef. Registering an InstanceCreator with Gson for this type may fix this problem. 代码1 private var listofMDetail: MutableList?=null mJson = Gson().toJson(listofMDetail) //Save 代码2 var mJson: String by PreferenceTool(this, getString(R.string.SavedJsonName) , “”) var aMListDetail= Gson().fromJson<MutableList>(mJson) inline fun Gson.fromJson(json: String) = this.fromJson(json, object: […]

从Kotlin代码修改json API

我是Kotlin的初学者,最近我学习了如何从json API 获取数据。 现在,我想知道如何将数据设置到API,例如,这里是我的API: { “username”: “Empty” “age”: 20 } 这是我的class级: @Parcel class Data { val username = “” val age = 0L fun loadFromJson(json: JsonObject) { username = readString(json, TAG_USERNAME) age = readLong(json, TAG_AGE) } companion object { private const val TAG_USERNAME = “username” private const val TAG_AGE = “age” } } 我正在寻找一个函数,它将采取一个数据类的参数,并会做类似的事情 json.set(data, […]

Kotlin数据类到JSON与spring/jackson

我试图公开一些数据类作为JSON对象,但有些不工作。 我有以下数据类: data class Link( @JsonProperty(“rel”) @JsonView(View.Bind::class) val rel: String, @JsonProperty(“method”) @JsonView(View.Bind::class) val method: HttpMethod, @JsonProperty(“href”) @JsonView(View.Bind::class) val href: String) data class MetaData(val status: HttpStatus) { @JsonView(View.Bind::class) @JsonProperty(“status_code”) fun getStatusCode(): Int { return status.value() } @JsonView(View.Bind::class) @JsonProperty(“status_desc”) fun getStatusDesc(): String { return status.name } } data class Payload( @JsonView(View.Bind::class) @JsonProperty(“payload”) val payload: Any, @JsonProperty(“_meta”) @JsonView(View.Bind::class) […]

Kotlin使用Gson将json字符串转换为ojbect列表

我有一个问题,Kotlin编写从json字符串转换为对象列表的代码。 在Java中通常是这样的: Gson gson = new Gson(); Type type = new TypeToken<List>() {}.getType(); List measurements = gson.fromJson(json, type); return measurements; 然而在Kotlin,当我尝试像这样: val gson = Gson() val type: Type = TypeToken<List>{}.type val measurements : List = gson.fromJson(text, type) return measurements IDE Android Studio强调为TypeToken所说的错误: 无法访问”:它是public / package / in’TypeToken’ 也强调说错误: types不匹配。 必需:键入! 发现:()→单位 那么有没有一种解决方案可以让Kotlin工作?

Jackson @JsonProperty注释用于kotlin数据类的使用

kotlin 1.2.10 jackson-module-kotlin:2.9.0 我在kotlin中有以下数据类: data class CurrencyInfo( @JsonProperty(“currency_info”) var currencyInfo: CurrencyInfoItem? ) @JsonInclude(JsonInclude.Include.NON_NULL) data class CurrencyInfoItem( @JsonProperty(“iso_4217”) var iso4217: String?, @JsonProperty(“name”) var name: String?, @JsonProperty(“name_major”) var nameMajor: String?, @JsonProperty(“name_minor”) var nameMinor: String?, @JsonProperty(“i_ma_currency”) var iMaCurrency: Int?, @JsonProperty(“i_merchant_account”) var iMerchantAccount: Int?, @JsonProperty(“i_x_rate_source”) var iXRateSource: Int?, @JsonProperty(“base_units”) var baseUnits: Double?, @JsonProperty(“min_allowed_payment”) var minAllowedPayment: Int?, @JsonProperty(“decimal_digits”) var decimalDigits: […]

与gson的Kotlin parcelize问题

我正在使用@parcelize for gson 这是我的class @Parcelize data class CommunityModel(@SerializedName(“post”) val post: PostModel, @SerializedName(“is_liked”) val isLiked: Boolean, @SerializedName(“post_like”) val postLike: QuestionModel, @SerializedName(“polling_options”) val pollingOptions: List, @SerializedName(“post_polled”) val postPolled: Boolean) : Parcelable 我得到错误Unable to invoke no-args constructor for class. Register an InstanceCreator with Gson for this type may fix this problem. Unable to invoke no-args constructor for class. […]