从复杂的JSON数据创建哈希图



我正在接收这些复杂的JSON数据,需要帮助将其转换为哈希图以存储到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'
        }]
      }]  
    }

此问题有 2 种可能的解决方案。首先是要有递归类结构,但你已经有了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安全。

最新更新