java 将 ArrayList 转换为 JSON - Android

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/25013255/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-11-02 07:09:03  来源:igfitidea点击:

Converting ArrayList to JSON - Android

javaandroidjson

提问by user3800832

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

我有一个数组列表和一个单独的字符串。我想将这些转换为 JSON 格式并期望它低于 json 格式。

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.

请帮忙。

回答by Stephen C

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.

但我同意其他答案,建议您使用像 GSON 这样的“映射”技术,而不是手动编码。特别是如果这变得更加复杂。

回答by Paras Mittal

You could use GSON.

你可以使用 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://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/

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

Hope this helps.

希望这可以帮助。

回答by Zaw Than oo

parse to JSONArray?

解析为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");