java - 从复杂的 JSON 数据创建 Hashmap

标签 java android json hashmap

我收到了这个复杂的 JSON 数据,需要帮助将其转换为 HashMap 以存储到 couchbaselite 文档数据库中。然后将使用 Couchbase lite 查询来查询其他功能。我使用改造接收 JSON 数据,并设计了模型类来处理从 API 接收的数据。

  {
      'project_id': 'PROJ10001',
      'project_name':'Sample Project',
      'forms': [
      {
        'form_name': 'main',
        'form_id': 'FORM001',
        'variables': [
        {
          'id':'VAR0001',
          'variable_name': 'firstname',
          'variable_label': 'First Name',
          'input_type': 'text',
          'hidden_text': '',
          'required': '1',
          'options': {},
          'default_value': 'n/a',
          'max_length': 50,
          'min_length': 2
          'pre_condition': '',
          'post_condition': '',
          'order_in_form': '1'
        },
        {
          'id':'VAR0002',
          'variable_name': 'age',
          'variable_label': 'What is your current age',
          'input_type': 'number',
          'hidden_text': '',
          'required': '1',
          'options': {},
          'default_value': '0',
          'max_length': 3,
          'min_length': 2
          'pre_condition': '',
          'post_condition': '',
          'order_in_form': '2'
        },
        {
          'id':'VAR0003',
          'variable_name': 'gender',
          'variable_label': 'Gender',
          'input_type': 'options',
          'hidden_text': '',
          'required': '1',
          'options': {
            'type':'list', 
            'multi':'0',
            items:[
              {'label':'Male','value:'m', skip:''},
              {'label':'Female','value':f', skip:''}
            ]
          },
          'default_value': '0',
          'max_length': 3,
          'min_length': 2
          'pre_condition': '',
          'post_condition': '',
          'order_in_form': '3'
         }]
      },
      {
        'form_name': 'male_form',
        'form_id': 'FORM002',
        'variables': [
        {
          'id':'VAR0004',
          'variable_name': 'beard',
          'variable_label': 'How many shirts do you have?',
          'input_type': 'number',
          'hidden_text': '',
          'required': '1',
          'options': {},
          'default_value': 'n/a',
          'max_length': 3,
          'min_length': 2
          'pre_condition': '',
          'post_condition': '',
          'order_in_form': '1'
        }]
      }]  
    }

最佳答案

这个问题有两种可能的解决方案。首先是要有递归类结构,但你已经有了JSONObject。 .另一个是这样的:

class Option {
    String label, value, skip;
}

class Variable {
    enum InputType {
        OPTIONS,
        NUMBER,
        TEXT
    }

    String id, name, label, hiddenText, defaultValue, preContidion, postCondition;
    int maxLength, minLength, orderInForm;
    boolean required;
    InputType inputType;
}

class OptionsVariable extends Variable {
    enum OptionsType {
        LIST    
    }

    OptionsType optionsType;
    boolean multi;
    ArrayList<Option> options;
}

class Form {
    String id, name;
    ArrayList<Variable> variables; // sort this by orderInForm
}

class Project {
    String id, name;
    ArrayList<Form> forms;
}

希望对您有所帮助!顺便说一句,如果您将整数转换为 JSON 中的字符串,您将无法避免 NumberFormatException

关于java - 从复杂的 JSON 数据创建 Hashmap,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48164228/

相关文章:

javascript - 如何添加到现有的 JSON.stringify 字符串

json - 如何在json对象中传递对象列表

Java:使用反射转储对象信息时如何避免循环引用?

java - Jackson JSON 生成器为缺失的对象创建空 JSON 值

安卓最有值(value)球员。谁关心从对话框和外部应用程序启动和获取数据?

java.lang.NoClassDefFoundError 与 HttpResponseCache 和 DiskLruCache

c# - 使用 Json.NET 反序列化字符串和对象的混合集合

java - 何时在 JAVA 的 glob 语法中使用 **(双星)

java - keycloak:谷歌登录字符编码错误

java - 单击标签仅查看具有相同标签的图片