Android Form data to JSONObjects
For each item in the list you need to do some like this:
JSONObject jsonObj = new JSONObject();try { jsonObj.put("title", item.getItemTitle()); jsonObj.put("content", item.getItemContent()); jsonObj.put("description", item.getItemDescription());} catch (JSONException e) { // TODO Auto-generated catch block e.printStackTrace();}
Another way is to use gson, given the item object, you only need to call
String jsonString = gson.toJson(item)
Here is the developer guide for Gsonhttps://sites.google.com/site/gson/gson-user-guide
Do you simply mean you want to create a JSONObject with these values?Did you try this:
JSONObject info = new JSONObject();try{ info.put("title",getData.getCharSequence("title")); info.put("subject",getData.getCharSequence("subject")););}catch(JSONException e){ e.printStackTrace();}
Thats it, you now have a JSONObject.
You can also create an Array of Information like this:
JSONObject info1 = new JSONObject();info1.put("firstName","newFirstName");info1.put("lastName","newLastName");JSONObject info2 = new JSONObject();info2.put("firstName","newFirstName");info2.put("lastName","newLastName");JSONArray myArray = new JSONArray();myArray.put(info1);myArray.put(info2);JSONObject newObj = JSONObject();newObj.put(myArray);
You can try this
try { JSONObjects json =new JSONObjects(); json.put(Key,value); json.put(Key,value); //and pass the data Bundle itembundle =new Bundle(); itembundle.putString("items",json.toString()); //and get data JSONObjects json=new JSONObjects(getArguments().getString("items")); String title=json.getString(Key); }catch (Exception e) { e.printStackTrace(); }