【问题标题】:Parsing Dynamic JSON Data with Gson and Store it in SQlite DB in Android使用 Gson 解析动态 JSON 数据并将其存储在 Android 的 SQlite DB 中
【发布时间】:2015-12-29 13:35:07
【问题描述】:

我在我的 Android 应用程序中从服务器收到此 JSON 响应,我想通过 Gson 解析此数据,在我的 Json 响应权限标签中可能包含 20-25 个数据,其键为 1、2, ...权限的每个值都包含由#键分隔的两个信息我必须只显示那些作为响应而进入权限的东西。解析的对象类(Pojo 或模型类必须Parcelable 实现) 我的应用程序也支持离线功能,所以需要建议我在解析时如何将数据同时存储在数据库中。

[{
    "permission": {
        "0": "Title#RaqStar",
        "1": "Desc#Reader of Raw Star",
        "3": "Tytpe#Entertainment",
        "4": "Rating#4.5"
    },
    "id": "233",
    "name": "Movie Raw Star"
}, {
    "permission": {
        "0": "Title#RaqStar",
        "3": "Tytpe#Entertainment",
        "4": "Rating#4.5"
    },
    "id": "233",
    "name": "Movie Raw Star"
}, {
    "permission": {
        "1": "Desc#Reader of Raw Star",
        "3": "Tytpe#Entertainment",
        "4": "Rating#4.5"
    },
    "id": "233",
    "name": "Movie Raw Star"
}, {
    "permission": {
        "1": "Desc#Reader of Raw Star",
        "4": "Rating#4.5"
    },
    "id": "233",
    "name": "Movie Raw Star"
}]

【问题讨论】:

  • 您使用什么代码来处理 JSON 数据?为什么当时不能将数据存储在数据库中?
  • 我只是简单地解析数据,现在它是字符串对象格式。
  • 你必须澄清。您是想将 JSON 数据存储为字符串,还是想拆分所有属性并将其作为(某种程度)标准化数据结构存储在数据库中?
  • 感谢您的支持好友我得到了答案。
  • @Abhishek 你找到解决方案了吗?

标签: android json sqlite gson parcelable


【解决方案1】:
    //  Whole response array I took as a String "data"
    try {
        JSONArray array = new JSONArray(data);
        for (int i = 0; i < array.length(); i++) {
             responseJSonObj = array.getJSONObject(i);
             System.out.println(
             generateTitleInfoMap(responseJSonObj.
             getJSONObject("permission")));
        }

    } catch (JSONException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }


public static HashMap<String, String> generateTitleInfoMap(JSONObject titleInfoMap) {
    try {
        HashMap<String, String> mapdataVal = new HashMap<String, String>();

        if (titleInfoMap.length() > 0) {
            Iterator keys = titleInfoMap.keys();
            while (keys.hasNext()) {
                // loop to get the dynamic key
                String currentDynamicKey = (String) keys.next();
                String[] ar = (titleInfoMap.getString(currentDynamicKey)).split("#");

                mapdataVal.put(ar[0], ar[1]);
            }
        }
        return mapdataVal;
    } catch (JSONException e) {

    }
    return null;
}

【讨论】:

猜你喜欢
  • 2011-10-02
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2017-08-26
  • 2015-07-16
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多