【问题标题】:How to Get JSON String to Int then to Arg Bundle?如何将 JSON 字符串转换为 Int 然后转换为 Arg Bundle?
【发布时间】:2012-06-12 17:29:14
【问题描述】:

我正在尝试传递从 JSON 字符串中提取的 Int 值以减少代码冗余。

在我的 JSON 文件中,“resFile”中有一个字符串值。我将此字符串存储到 TAG_RES_FILE 中,我想将它作为 Int 传递到 Bundle 中。

如果您查看我的代码,您会看到注释//TRY #1//。这按预期工作,但我需要 Int 来自存储我的 TAG_RES_FILE 的变量。在评论 //TRY #2// 只是我想要发挥作用的一个例子 - 显然它没有。在下一行中,我尝试将标记字符串转换为 Int 但这会导致运行时错误:

java.lang.NumberFormatException: Invalid int: "resFile"

我什至尝试将 0x7f060000(来自 R.java)放入 JSON 字符串。

所以我的问题是:我该如何做到这一点?我是在正确的轨道上还是应该采取完全不同的方式?

感谢您的帮助和输入 - 请在您的答案中显示代码示例。

JSON 字符串片段:

[
    {
        "_id": "1",
        "label": "A Lable",
        "title": "Some Title",
        "description": "Bla, bla, bla",
        "containerID": "Some container id",
        "isRawRes": "boolean value here",
        "resFile": "R.raw.advisory_circulators_sort_list"
    }, {. . .
]

在我的 HashMap 中:

// Parse the string to a JSON object
        for (int i = 0; i < json.length(); i++) {
            JSONObject json_data = json.getJSONObject(i);

            // Storing each json item in variable
            String id = json_data.getString(TAG_ID);
            String label = json_data.getString(TAG_LABEL);
            String title = json_data.getString(TAG_TITLE);
            String description = json_data.getString(TAG_DISCR);
            String containerID = json_data.getString(TAG_FRAG_ID);
            String isRawRes = json_data.getString(TAG_IS_RAW_RES);
            String resFile = json_data.getString(TAG_RES_FILE);

            // creating new HashMap
            HashMap<String, String> map = new HashMap<String, String>();

            // adding each child node to HashMap key => value
            map.put(TAG_ID, id);
            map.put(TAG_LABEL, label);
            map.put(TAG_TITLE, title);
            map.put(TAG_DISCR, description);
            map.put(TAG_FRAG_ID, containerID);
            map.put(TAG_IS_RAW_RES, isRawRes);
            map.put(TAG_RES_FILE, resFile);

            // adding HashList to ArrayList
            mList.add(map);
}

在我的 ListViews setOnItemClickListener 中:

public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
    . . .

        final Bundle args = new Bundle();

//TRY #1//int rawRes = R.raw.advisory_circulators_sort_list; <--I NEED TO GET THIS IN FROM MY TAG!!
//TRY #2//int rawRes = TAG_RES_FILE; <-- TO SOMETHING LIKE THIS!!
        int passResFile = Integer.parseInt(TAG_RES_FILE);//<--THIS GIVES A NPE!!
        args.putInt("KEY_RES_FILE", passResFile);

        bolean isRawRes = true;
        args.putBoolean("KEY_IS_RAW_RES", isRawRes);

        // Delayed to improve animations
        final Handler handler = new Handler();
        handler.postDelayed(new Runnable() {
            public void run() {
                ListViewFragment lvf = new ListViewFragment();
                lcFT.replace(R.id.listContainer, lvf).commit();
                lvf.setArguments(args);
            }
        }, 300);
    }

【问题讨论】:

    标签: android json string int bundle


    【解决方案1】:

    相反,只存储advisory_circulators_sort_list 而不是R.raw.advisory_circulators_sort_list。然后,要获取 Integer 标识符,请使用以下方法:

    int passResFile = getResources().getIdentifier( resFile, "raw", getPackageName() );
    

    【讨论】:

    • 你能看看over here吗?它与这个问题有关。Thnx
    猜你喜欢
    • 1970-01-01
    • 2018-06-03
    • 1970-01-01
    • 2013-09-10
    • 2019-10-10
    • 2021-10-05
    • 1970-01-01
    • 1970-01-01
    • 2019-10-16
    相关资源
    最近更新 更多