user3800832
user3800832

Reputation: 421

Converting ArrayList to JSON - Android

I have a Array List and a separate string. i want to convert these into JSON format and expect it below json format.

Expected format,

{  
"last_sync_date": "2014-06-30 04:47:45",
"recordset": [
    {
        "contact_group": {
            "guid": "y37845y8y",
            "name": "Family",        
            "description": "Family members",              
            "isDeleted": 0        
        } 
    },
    {
        "contact_group": { 
            "guid": "gt45tergh4",        
            "name": "Office",        
            "description": "Office members",              
            "isDeleted": 0        
        } 
    } 
]
} 

i used this way bt it's wrong,

public void createGroupInServer(Activity activity, String lastSyncDateTime, ArrayList<ContactGroup> groups)
        throws JSONException {

    // create json object to contact group
    JSONObject syncDateTime = new JSONObject();
    syncDateTime.putOpt("last_sync_date", lastSyncDateTime);

    JSONArray jsArray = new JSONArray("recordset");

    for (int i=0; i < groups.size(); i++) {
        JSONObject adsJsonObject = new JSONObject("contact_group");
        adsJsonObject = jsArray.getJSONObject(i);
        adsJsonObject.put("guid", groups.get(i).getGroupId());
        adsJsonObject.put("name", groups.get(i).getGroupName());
        adsJsonObject.put("isDeleted", groups.get(i).getIsDeleted());
}

please help.

Upvotes: 3

Views: 20938

Answers (3)

Stephen C
Stephen C

Reputation: 719576

You are mostly on the right track ... but there are a few mistakes:

public JSONObject createGroupInServer(
        Activity activity, String lastSyncDateTime,
        ArrayList<ContactGroup> groups)
        throws JSONException {

    JSONObject jResult = new JSONObject();
    jResult.putOpt("last_sync_date", lastSyncDateTime);

    JSONArray jArray = new JSONArray();

    for (int i = 0; i < groups.size(); i++) {
        JSONObject jGroup = new JSONObject();
        jGroup.put("guid", groups.get(i).getGroupId());
        jGroup.put("name", groups.get(i).getGroupName());
        jGroup.put("isDeleted", groups.get(i).getIsDeleted());
        // etcetera

        JSONObject jOuter = new JSONObject();
        jOuter.put("contact_group", jGroup);

        jArray.put(jOuter);
    }

    jResult.put("recordset", jArray);
    return jResult;
}

But I agree with the other Answers that suggest you use a "mapping" technology like GSON rather than coding this by hand. Especially if this gets more complicated.

Upvotes: 6

Paras Mittal
Paras Mittal

Reputation: 1149

You could use GSON.

It provides a lot of functionality and its very simple to use.

Have a look at these examples.

http://kylewbanks.com/blog/Tutorial-Android-Parsing-JSON-with-GSON

http://www.techrepublic.com/blog/software-engineer/use-gson-to-work-with-json-in-your-android-apps/

Hope this helps.

Upvotes: 1

Zaw Than oo
Zaw Than oo

Reputation: 9935

parse to JSONArray?

JSONArray jsonArray = (JSONArray)new JSONParser().parse(your json string);

For your code

    JSONObject jsonObject = (JSONObject)new JSONParser().parse(your json string);
    JSONArray array = (JSONArray)jsonObject.get("recordset");   

Upvotes: 1

Related Questions