Home > other >  Json String to dataClass
Json String to dataClass

Time:08-26

I need to convert some json string that im reciving from okhttp websocket binance connection to data class to manipulate the data

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}

String recived:

    {
  "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
}

which is the best implementation? Thanks!

CodePudding user response:

First of all,you need to have a protocol with server end, and settle down all the message IDL models. Then you can use some tools to transfer IDL to Java Model. as for json to model, you can use https://www.jsonschema2pojo.org/ But I suggest you use protobuf. which is more effective than json. https://square.github.io/wire/

CodePudding user response:

Gson dependency

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

Data class of your json is like below

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

)

convert your json string to data model

var gson = Gson()

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

use modelClassOfJsonString and if you went to convert model class to json string use

 var stringOfmodel : String = gson.toJson(modelClassOfJsonString)
  • Related