java - Convert a Bundle to JSON -


I bundle in a JSONObject in addition to one intent to convert so that I can pass it to / from javascript.

Is this a quick or the best way to do the conversion? It will be okay if not all possible bundles will work.

After

you can use bundle #kitset () present in a bundle To get a list of keys, you can iterate through those keys and add each key-value pair to a pair. JSONObject

  JSONObject json = New JSONObject (); Set up & lt; String & gt; Key = bundle.kitset (); (String key: keys) {try {// json.put (key, bundle.tate (key)); See edit json.put below (key, JSONObject.wrap (bundle.txt (key)); } Hold (JSONException E) {// handle exception exception}}  

Note that JSONObject # will be required to hold a JSONException .

Edit:

It was reported that the archive and were not handled in the previous code Type very well if you are using API 19 or more, then there is a JSONObject # wrap method that is important to you if it will help. From:

Wrap an object, if necessary. If the object is empty, then return to the tap object. If it is an array or a collection, wrap it in a Jason Array. If it is a map, then wrap it in a JSONObject if it is a standard property (double, string, et al) then it is already wrapped. Otherwise, if it comes from one of the Java packages, then change it to a string. And if it does not, try wrapping it in JSONObject. If the wrapping fails, then the return is zero.


Comments