0

这是一个我似乎无法解决的逻辑问题,但我我已经接近了。我从JSON响应中获取值并将它们存储在 a中,HashMap然后将它们添加HashMapArrayList.

做背景

protected String doInBackground(String... args) {

            // getting JSON string from URL
            companyName = cn.getText().toString();
            projectName = pn.getText().toString();
            String componentName = (String) ab.getSelectedTab().getText();

            List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>(3);
            nameValuePairs.add(new BasicNameValuePair("company", companyName));
            nameValuePairs.add(new BasicNameValuePair("project", projectName));
            nameValuePairs.add(new BasicNameValuePair("component",
                    componentName));

            JSONObject json = jParser.makeHttpRequest(url, "POST",
                    nameValuePairs);

            // Check your log cat for JSON response
            Log.d("All Questions: ", json.toString());

            try {
                // Checking for SUCCESS TAG
                int success = json.getInt(TAG_SUCCESS);

                if (success == 1) {
                    Log.v("RESPONSE", "Success!");
                    // products found: getting Array of Questions
                    questions = json.getJSONArray(TAG_QUESTIONS);

                    // looping through All Questions
                    for (int i = 0; i < questions.length(); i++) {

                        JSONObject c = questions.getJSONObject(i);

                        // Storing each JSON item in variable
                        String name = c.getString(TAG_NAME);
                        String field = c.getString(TAG_FIELD);
                        String value = c.getString(TAG_VALUE);

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

                        // adding each child node to HashMap key => value
                        map.put(TAG_NAME, name);
                        map.put(TAG_FIELD, field);
                        map.put(TAG_VALUE, value);
                        for (String key: map.keySet()) {
                            System.out.println("key : " + key);
                            System.out.println("value : " + map.get(key));
                        }
                        infoList.add(map);
                    }

                } else {
                    // no products found
                    Log.v("ERROR", "No JSON for you!");
                }
            } catch (JSONException e) {
                e.printStackTrace();
            }
            return null;
        }

现在上面for loop从我的打印以下JSON

06-03 19:35:29.928: I/System.out(9691): key : option_value
06-03 19:35:29.928: I/System.out(9691): value : 
06-03 19:35:29.928: I/System.out(9691): key : field_type
06-03 19:35:29.928: I/System.out(9691): value : Text Field
06-03 19:35:29.928: I/System.out(9691): key : display_name
06-03 19:35:29.928: I/System.out(9691): value : Store #
06-03 19:35:29.928: I/System.out(9691): key : option_value
06-03 19:35:29.928: I/System.out(9691): value : 
06-03 19:35:29.928: I/System.out(9691): key : field_type
06-03 19:35:29.928: I/System.out(9691): value : Text Field
06-03 19:35:29.928: I/System.out(9691): key : display_name
06-03 19:35:29.938: I/System.out(9691): value : Address
06-03 19:35:29.938: I/System.out(9691): key : option_value
06-03 19:35:29.938: I/System.out(9691): value : Education
06-03 19:35:29.938: I/System.out(9691): Health
06-03 19:35:29.938: I/System.out(9691): Computers
06-03 19:35:29.938: I/System.out(9691): Food
06-03 19:35:29.938: I/System.out(9691): Retail
06-03 19:35:29.938: I/System.out(9691): Other
06-03 19:35:29.938: I/System.out(9691): key : field_type
06-03 19:35:29.938: I/System.out(9691): value : Drop Down Menu
06-03 19:35:29.938: I/System.out(9691): key : display_name
06-03 19:35:29.938: I/System.out(9691): value : Type of Business
06-03 19:35:29.938: I/System.out(9691): key : option_value
06-03 19:35:29.938: I/System.out(9691): value : Yes
06-03 19:35:29.938: I/System.out(9691): No
06-03 19:35:29.938: I/System.out(9691): key : field_type
06-03 19:35:29.938: I/System.out(9691): value : Radio
06-03 19:35:29.938: I/System.out(9691): key : display_name
06-03 19:35:29.938: I/System.out(9691): value : Is this business good?
06-03 19:35:29.938: I/System.out(9691): key : option_value
06-03 19:35:29.938: I/System.out(9691): value : Yes
06-03 19:35:29.938: I/System.out(9691): No
06-03 19:35:29.938: I/System.out(9691): key : field_type
06-03 19:35:29.938: I/System.out(9691): value : Check Box
06-03 19:35:29.938: I/System.out(9691): key : display_name
06-03 19:35:29.938: I/System.out(9691): value : Are they nice people?

JSON

{
    "questions": [
        {
            "display_name": "Store #",
            "field_type": "Text Field",
            "option_value": ""
        },
        {
            "display_name": "Address",
            "field_type": "Text Field",
            "option_value": ""
        },
        {
            "display_name": "Type of Business",
            "field_type": "Drop Down Menu",
            "option_value": "Education\r\nHealth\r\nComputers\r\nFood\r\nRetail\r\nOther"
        },
        {
            "display_name": "Is this business good?",
            "field_type": "Radio",
            "option_value": "Yes\r\nNo"
        },
        {
            "display_name": "Are they nice people?",
            "field_type": "Check Box",
            "option_value": "Yes\r\nNo"
        }
    ],
    "success": 1
}

现在onPostExecute()我需要遍历我ArrayList命名的“infoList”,然后获取HashMap值。基于该信息,我需要创建Views. 所以我的代码应该看起来像这样,但我做错了。

protected void onPostExecute(String string) {
    // dismiss the dialog
    pDialog.dismiss();  
    for (int i = 0; i < infoList.size(); i++) {
        // get HashMap, how? i.toString()?
        for (String key: map.keySet()) {
        if (map.get(key).equals("Radio")) {
                //create RadioButtons, setTexts to option_value values         
            } else if (map.get(key).equals("Text Field")) {
                //create EditText
            } else if (map.get(key).equals("Check Box")) {
                //create CheckBox's, setTexts to option_value values
            } else if (map.get(key).equals("Drop Down Menu")) {
                //create Spinner, place option_value values into array and populate
            }
         }
    }

那么我是否让这变得比需要的更难?我觉得他们必须是一个更简单的方法。如果不是,我将不胜感激一些帮助写这篇文章for loop

根据评论进行编辑

我希望这可以膨胀一个fragment看起来像这样的布局

Store # ------------------ <EditText>
Address ------------------ <EditText>
Is this business good? --- <RadioButton>

等等等等。RadioButtonorCheckbox将由 optional_values 设置它们的文本

完成后,我会将其发送回最初从中获取的数据库。

4

2 回答 2

2

为您的问题构建一个 POJO 类:

Class Questions{
private String Name;
private String field_type;
private ArrayList option_value;
<Getter and Setters>
}

将其用作 POJO 类。并将其添加到 ArrayList。

只有在需要时才使用地图。使用 POJO/Business Objects 传输数据就足够了,而且最适合。

请让我知道你的想法。

于 2013-06-04T00:09:50.037 回答
1

与使用不合适的库相比,您使自己变得更加困难。

使用Jackson,您将更轻松地做您想做的事情。首先,JsonNode从您的输入源中获取您的信息,这通常通过以下方式完成ObjectMapper

// "mapper" is an already initialized ObjectMapper
final JsonNode response = mapper.readTree(yourURL); // catch, treat IOException

现在,阅读success

if (response.get("success").intValue() != 1 )
    // deal with failure

成功?处理所有值:

for (final JsonNode question: response.get("questions"))
    // do something with "question", which is an element of the "questions" array

请参阅JsonNode. 在 Java 中没有更好的类用于访问/导航 JSON。

之后,如果您需要/想要,您可以使用注释将每个问题反序列化为 POJO 并进行处理。这是我为 JSON Patch 操作所做的示例:

https://github.com/fge/json-patch/blob/master/src/main/java/com/github/fge/jsonpatch/JsonPatchOperation.java

JsonPatchOperation该文件将根据"op"成员值是什么生成不同的实现;例如:

{ "op": "remove", "path": "/foo" }

将生成一个RemoveOperation.

杰克逊一开始似乎相当令人生畏。但是一旦你知道如何使用它,它就轻而易举了。

于 2013-06-04T00:16:02.820 回答