Tag: json

Log4j2不能使用json配置文件

似乎log4j试图通过XML解析器解析.json配置文件,但是为什么? 我通过命令行参数设置log4j配置文件: -Dlog4j.configurationFile=src/log4j2.json 文件内容: { "configuration": { "appenders": { "Console": { "name": "Console", "target": "SYSTEM_OUT", "PatternLayout": { "pattern": "%date %thread %logger %message" } } }, "loggers": { "root": { "level": "debug", "appender-ref": { "ref": "Console" } } } } } 我得到: [Fatal Error] log4j2.json:1:1: Content is not allowed in prolog. ERROR StatusLogger Error parsing D:\Dev\HDelphiCrawler\src\log4j2.json […]

如何从url中获取kotlin中的数组json

在Android工作室“ kotlin ”新,我试图从url数据json ,内容JSONObject和JSONArray ,我的代码与JSONObject工作正常,但我仍然有JSONArray问题。 这是我的任何一个代码可以帮助我如何从我的网址获取JSONArray fun buclick(view:View){ val flightSearch=editText2.text.toString() val url="xxxxxxx-flight=$flightSearch" MyAsyncTask().execute(url) } inner class MyAsyncTask:AsyncTask<String,String,String>(){ override fun onPreExecute() { super.onPreExecute() } override fun doInBackground(vararg p0: String?): String { //تمرير بيانات هنا try { val url=URL(p0[0]) val urlConnect = url.openConnection() as HttpURLConnection urlConnect.connectTimeout=500 val dataJsonAsString=convertStreanToString(urlConnect.inputStream) publishProgress(dataJsonAsString) }catch (ex:Exception){ } return "" } override fun […]

Kotlin将对象转换为另一个对象

http://cm08323.tmweb.ru/android/determinace/json/images.json我有一些JSON到服务器。 我的数据库类: @Entity data class Image(val url: String = "", val urlAnswer: String = "", val race: String = "") : DisplayableItem, Persistable api接口: interface ImagesAdi { @GET("android/determinace/json/images.json") fun getImages(): Single<List<ImagesResponse>> } … class ImagesResponse( val images: List<ImageSingle>) class ImageSingle(val url: String, val url_answer: String, val race: String) 我的映射器用于转换ImagesResponse到Image : @Singleton class ChooseRaceMapper @Inject constructor() […]

kotlin数据类HttpMessageNotReadableException

我正在尝试这个邮递员的代码,但没有任何作品,为什么? 我发送的是: { "name":"front_msel", "gitlabId": "83", "fichierVersion":"VERSION" } 我的弹簧控制器: @RestController @RequestMapping("/projects") class ProjectController(val projectRepository: ProjectRepository) { private val log = LoggerFactory.getLogger(ProjectController::class.java) @PostMapping() fun saveProject(@RequestBody payload: Project): String { log.info("project: '{}'", payload.toString()) return projectRepository.save(payload).gitlabId?:"-1" } } 我得到: { "timestamp": 1505917417221, "status": 400, "error": "Bad Request", "exception": "org.springframework.http.converter.HttpMessageNotReadableException", "message": "JSON parse error: Can not construct instance of […]

从Spark + Kotlin的POST正文获取JSON数据

我正在玩Kotlin和Spark,创建一个REST风格的Web服务。 不过,我正在努力解析一个JSON POST请求。 我有以下端点… post("") { req, res -> var objectMapper = ObjectMapper() println(req.body()) val data = objectMapper.readValue(req.body(), User::class.java) usersDao.save(data.name, data.email, data.age) res.status(201) "okies" } 然而,我得到一个500错误,这不是实际上打印错误,只是返回一个500。 它似乎是这一行val data = objectMapper.readValue(req.body(), User::class.java) 。 我试图将json正文转换为一个用户对象。 这是我的用户对象… data class User(val name: String, val email: String, val age: Int, val id: Int)

如何使用Gson反序列化继承的Kotlin数据类

在Android应用程序中,我需要为具有单一抽象级别的Kotlin数据类反序列化Json数据。 但我没有任何想法,在构造函数中放置正确的属性。 作为一个简单的版本,让我们说我有一个形状: abstract class Shape(open val x: Int, open val y: Int) 有两个派生 data class Circle(val radius: Int, override val x: Int, override val y: Int): Shape(x, y) 和 data class Square(val width: Int, override val x: Int, override val y: Int): Shape(x, y) 所以我的目标是,不要实例化一个形状。 所以,总是反序列化它的派生。 后来我需要处理其他类中的一些集合属性,如: val shapes: List<Shape> 但我也必须知道每个元素的派生类型。 当我试图用Gson反序列化给定的例子 val square […]

Moshi + Kotlin + SealedClass

有没有反序列化json使用的方法 sealed class Layer data class ShapeLayer(var type: LayerType) : Layer data class TextLayer(var type: LayerType) : Layer data class ImageLayer(var type: LayerType) : Layer LayerType只是一些枚举,可以用来区分这个对象应该具有哪种类型。 我想我可以这样添加Adapter: class LayerAdapter{ @FromJson fun fromJson(layerJson: LayerJson): Layer { return when (layerJson.layerType) { LayerType.SHAPE -> PreCompLayer() LayerType.SOLID -> SolidLayer() LayerType.Text -> TextLayer() } } } 其中LayerJson将是所有LayerType的每个可能字段的对象。 现在的问题是: 无法序列化抽象类com.example.models.layers.Layer 我可以尝试使用接口,但是我不认为在这里使用空接口是正确的。

在kotlin中使用Gson来解析json数组

试图解析kotlin中的json数组,使其适用于WeatherObject对象的单个json对象(下面的代码片段) { "coord": { "lon": -2.93, "lat": 43.26 }, "weather": [{ "id": 802, "main": "Clouds", "description": "scattered clouds", "icon": "03d" }], "main": { "temp": 283.681, "temp_min": 283.681, "temp_max": 283.681, "pressure": 991.72, "sea_level": 1034.92, "grnd_leve": 991.72, "humidity": 98 }, "wind": { "speed": 1.07, "deg": 144.001 }, "dt": 1429773245, "id": 3128026, "name": "Bilbao", "cod": 200 } 但不知道如何做到这一点,如果json是一个相同的json对象的数组,即 […]

Kotlin或Swift能够像Javascript一样解析JSON吗?

如果我使用JavaScript(或TypeScript),我可以做以下(只是想法); object = JSON.parse(jsonString) 而且我可以像这样使用它, alert(object.property); 超级简单。 如果我使用Java,我需要创建类并解析它来使用它。 我明白。 Kotlin和Swift怎么样? 他们有可选的类型,为什么单行,类似Javascript的简单解析不存在,或者它呢? (甚至没有数据类或通过JSON的属性)

Gson无法解析Kotlin中的字符串json格式数据

我正在Kotlin写一个应用程序。 我有一个来自web服务的原始JSON字符串,我需要使用它与Gson。 我正在这样做: val gson = Gson() val friends = gson.fromJson(response.rawResponse, JsonElement::class) 但编译器无法找到正确的fromJson方法重载,而现在可用( fromJson(json: String!, typeOfT: Type!) )。 这是错误的: Error:(65, 50) None of the following functions can be called with the arguments supplied: public open fun <T : Any!> fromJson(json: JsonElement!, classOfT: Class<JsonElement!>!): JsonElement! defined in com.google.gson.Gson public open fun <T : Any!> fromJson(json: JsonElement!, […]