从websocket到dataClass的Json字符串



我需要将我从okhttp websocket二进制连接中收到的一些json字符串转换为数据类来操作数据

override fun onMessage(webSocket: WebSocket, text: String) {
//convert string "text" to dataclass 
Log.d("Websocket", text)
}
Log: D/Websocket: {"e":"24hrTicker","E":1661477897574,"s":"BNBUSDT","p":"0.30000000","P":"0.100","w":"301.82156206","x":"298.60000000","c":"298.90000000","Q":"1.06900000","b":"298.80000000","B":"353.26400000","a":"298.90000000","A":"358.58100000","o":"298.60000000","h":"307.50000000","l":"296.00000000","v":"412516.01400000","q":"124506227.71920000","O":1661391497474,"C":1661477897474,"F":581001589,"L":581229754,"n":228166}

收到的字符串:

{
"e": "24hrTicker",  // Event type
"E": 123456789,     // Event time
"s": "BNBBTC",      // Symbol
"p": "0.0015",      // Price change
"P": "250.00",      // Price change percent
"w": "0.0018",      // Weighted average price
"x": "0.0009",      // First trade(F)-1 price (first trade before the 24hr rolling window)
"c": "0.0025",      // Last price
"Q": "10",          // Last quantity
"b": "0.0024",      // Best bid price
"B": "10",          // Best bid quantity
"a": "0.0026",      // Best ask price
"A": "100",         // Best ask quantity
"o": "0.0010",      // Open price
"h": "0.0025",      // High price
"l": "0.0010",      // Low price
"v": "10000",       // Total traded base asset volume
"q": "18",          // Total traded quote asset volume
"O": 0,             // Statistics open time
"C": 86400000,      // Statistics close time
"F": 0,             // First trade ID
"L": 18150,         // Last trade Id
"n": 18151          // Total number of trades
}

哪个是最好的实现?谢谢

首先,您需要有一个与服务器端的协议,并确定所有消息IDL模型。然后可以使用一些工具将IDL转换为Java模型。至于要建模的json,您可以使用https://www.jsonschema2pojo.org/但我建议你使用protobuf。它比json更有效。https://square.github.io/wire/

Gson依赖

implementation 'com.google.code.gson:gson:2.9.1'

json的数据类如下

import com.google.gson.annotations.SerializedName

data class Websocket(
@SerializedName("e" ) var e : String? = null,
@SerializedName("E" ) var E : Int?    = null,
@SerializedName("s" ) var s : String? = null,
@SerializedName("p" ) var p : String? = null,
@SerializedName("P" ) var P : String? = null,
@SerializedName("w" ) var w : String? = null,
@SerializedName("x" ) var x : String? = null,
@SerializedName("c" ) var c : String? = null,
@SerializedName("Q" ) var Q : String? = null,
@SerializedName("b" ) var b : String? = null,
@SerializedName("B" ) var B : String? = null,
@SerializedName("a" ) var a : String? = null,
@SerializedName("A" ) var A : String? = null,
@SerializedName("o" ) var o : String? = null,
@SerializedName("h" ) var h : String? = null,
@SerializedName("l" ) var l : String? = null,
@SerializedName("v" ) var v : String? = null,
@SerializedName("q" ) var q : String? = null,
@SerializedName("O" ) var O : Int?    = null,
@SerializedName("C" ) var C : Int?    = null,
@SerializedName("F" ) var F : Int?    = null,
@SerializedName("L" ) var L : Int?    = null,
@SerializedName("n" ) var n : Int?    = null
)

将json字符串转换为数据模型

var gson = Gson()
val modelClassOfJsonString: Websocket = gson.fromJson("YOUR JSON STRING", Websocket::class.java)

使用modelClassOfJsonString如果要将模型类转换为json字符串,请使用

var stringOfmodel : String = gson.toJson(modelClassOfJsonString)

最新更新