I have a problem when I want to sending data using byte format in UDP protocol, the problem is when I try to create a data with type json object, I can't get the byte format of my data this is my sample code:
JSONObject obj = new JSONObject(); obj.put("name", "foo"); obj.put("num", new Integer(100)); obj.put("balance", new Double(1000.21)); obj.put("is_vip", new Boolean(true)); obj.put("nickname",null); sendData = obj.getBytes(); //this is error because not have methos getBytes();
i know my problem but i can't found how to convert json object to byte, any suggestion ?
JSONObject obj = new JSONObject(); obj. put("name", "foo"); obj. put("num", new Integer(100)); obj. put("balance", new Double(1000.21)); obj.
JSON does not support that. Use Base64. That is your library supporting it, not JSON itself. The byte array wont be stored as byte array in the JSON, JSON is a text format meant to be human readable.
JSON (JavaScript Object Notation) is a lightweight, text-based, language-independent data exchange format that is easy for humans and machines to read and write. JSON can represent two structured types: objects and arrays. An object is an unordered collection of zero or more name/value pairs.
JavaScript Object Notation (JSON) is a standard text-based format for representing structured data based on JavaScript object syntax. It is commonly used for transmitting data in web applications (e.g., sending some data from the server to the client, so it can be displayed on a web page, or vice versa).
Get the bytes of the string:
obj.toString().getBytes(theCharset);
Assuming the JSONObject you mention is from this, you can get the bytes like below
sendData = obj.toString().getBytes("utf-8");
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With