3

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.

2
  • Have you considered using Jackson or GSON to write less bugs? If not, you should. The above task will be simple. Commented Jul 29, 2014 at 10:37
  • @ZhenxiaoHao - No. The Question is clearly about turning Java objects into JSON. The JSON he has supplied is clearly intended to be what the sample Java code generates. Commented Jul 29, 2014 at 11:40

3 Answers 3

6

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.

Sign up to request clarification or add additional context in comments.

Comments

1

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");   

Comments

1

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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.