首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >将JSON转换为模型对象Android

将JSON转换为模型对象Android
EN

Stack Overflow用户
提问于 2014-03-02 22:41:11
回答 1查看 174关注 0票数 0

我刚开始在Android中使用JSON,并且在从JSON创建模型对象时遇到了一些问题。如果任何人知道任何好的教程或资源,我将非常感谢他们。到目前为止,我一直在看关于Vogella的教程和Bignerdranch Android一书。

我可以引入JSON并创建一个JSONobject,但是我的调查没有保存。

下面是我要解析的JSON:

代码语言:javascript
运行
复制
    [
        {
            "title": "Pepsi or Coke?",
            "id": 1,
            "questions": [
                {
                    "id": 1,
                    "title": "Which pop do you prefer?",
                    "single_response": true,
                    "answers": [
                        {
                            "title": "Pepsi",
                            "id": 1
                        },
                        {
                            "title": "Coke",
                            "id": 2
                        },
                        {
                            "title": "Mountain Dew",
                            "id": 3
                        }
                    ]
                },
                {
                    "id": 2,
                    "title": "What's your age?",
                    "single_response": true,
                    "answers": [
                        {
                            "title": "18-24",
                            "id": 4
                        },
                        {
                            "title": "25-34",
                            "id": 5
                        },
                        {
                            "title": "35-50",
                            "id": 6
                        },
                        {
                            "title": "50+",
                            "id": 7
                        }
                    ]
                },
                {
                    "id": 3,
                    "title": "What's your political association?",
                    "single_response": true,
                    "answers": [
                        {
                            "title": "Republican",
                            "id": 8
                        },
                        {
                            "title": "Democrat",
                            "id": 9
                        }
                    ]
                }
            ]
        }
    ]

我像这样检索json:

代码语言:javascript
运行
复制
byte[] getUrlBytes(String urlSpec) throws IOException {
            URL url = new URL(urlSpec);
            HttpURLConnection connection = (HttpURLConnection) url.openConnection();

            try {
                ByteArrayOutputStream out = new ByteArrayOutputStream();
                InputStream in = connection.getInputStream();

                if (connection.getResponseCode() != HttpURLConnection.HTTP_OK) {
                    return null;
                }

                int bytesRead = 0;
                byte[] buffer = new byte[1024];
                while ((bytesRead = in.read(buffer)) > 0) {
                    out.write(buffer, 0, bytesRead);
                }
                out.close();
                return out.toByteArray();
            } finally {
                connection.disconnect();
            }
        }

        public String getUrl(String urlSpec) throws IOException {
            return new String(getUrlBytes(urlSpec));
        }

下面是我解析它的地方:

代码语言:javascript
运行
复制
public ArrayList<Survey> getSurveys(String apiKey) throws JSONException {
    ArrayList<Survey> surveys = new ArrayList<Survey>();

    try {
        String url = Uri.parse(ENDPOINT).buildUpon().appendQueryParameter("auth_token", apiKey).build().toString();
        String jsonString = getUrl(url);
        Log.i(TAG, "Received json string: " + jsonString);

        try {
            JSONArray array = new JSONArray(jsonString);

            for (int i = 0; i < array.length(); i ++) {
                JSONObject object = array.getJSONObject(i);
                Log.i(TAG, "Object is: " + object.toString());
            }

            for (int i = 0; i < array.length(); i++) {
                Survey survey = new Survey(array.getJSONObject(i));
                surveys.add(survey);
                Log.i(TAG, "Survey is: " + survey.toString());
            }  
        } catch (Exception e) {
            Log.e(TAG, "Survey didn't save");
        }

    } catch (IOException ioe) {
        Log.e(TAG, "Failed to retrieve surveys: " + ioe);
    }
    return surveys; 
}

我假设问题出在我的创建调查方法本身,因为JSONobjects是正确创建的,但是调查没有保存。你知道我哪里错了吗?

代码语言:javascript
运行
复制
public Survey(JSONObject json) throws JSONException {
        mId = UUID.fromString(json.getString("id"));
        if (json.has("title")) {
            mTitle = json.getString("title");
        }
}

一如既往,任何帮助都是非常感谢的!

EN

回答 1

Stack Overflow用户

发布于 2014-03-03 05:19:19

事实证明,问题是我试图将JSON中发送的int id转换为UUID。

只要数据类型匹配,就可以正确地创建项。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/22128982

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档