Gravity Gravity - 6 months ago 33
JSON Question

JSON Serialize Object Array Android

I have a very simple Object Array

Object[] args = new Object[] {"002"};


My goal is to JSON serialize this Object Array in Android and get the result as a byte array. There are two classes for JSON Serialization in Android.
JSONArray
and
JSONObject
. Which class should I use?

Spefically I am not sure how to use the

JSONObject json = new JSONObject();
json.put("key", "value");


routine in this case.

Answer

You should use a JSONArray for this

JSONArray jsonArray = new JSONArray();
jsonArray.put("002");
byte[] bytes = jsonArray.toString().getBytes()