javaandroidjsonstringibeacon

How to convert string to JSON payload in Java


I wonder how to convert string to JSON in Java.

I want to convert

iBeaconDevice{address='F1:E4:E4:AD:24:50', uniqueId='null', proximityUUID=b9407f30-f5f8-466e-aff9-25556b57fe6d, major=35823, minor=10411, rssi=-56, shuffled=false}

to JSON payload like below:

{
  "address": "F1:E4:E4:AD:24:50",
  "uniqueId": "null",
  "proximityUUID": "b9407f30-f5f8-466e-aff9-25556b57fe6d",
  "major": 35823,
  "minor": 10411,
  "rssi": -56,
  "shuffled": "false",
  "timestamp": 1679470296838,
}

Solution

  • try to use Gson.

    val json = Gson().toJson(your_object)
    

    or

    Gson gson = new Gson();
    String json = gson.toJson(your_object);