【问题标题】:Create Hashmap from complicated JSON data从复杂的 JSON 数据创建 Hashmap
【发布时间】:2018-01-09 08:41:00
【问题描述】:

我收到了这个复杂的 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'
        }]
      }]  
    }

【问题讨论】:

  • 您在尝试转换时遇到了什么问题?
  • 我是手动做的,我想要一个快速的方法,通过循环或其他方法

标签: java android json hashmap


【解决方案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 是不安全的。

【讨论】:

    猜你喜欢
    • 2021-10-27
    • 1970-01-01
    • 1970-01-01
    • 2012-03-01
    • 1970-01-01
    • 1970-01-01
    • 2018-12-12
    • 1970-01-01
    • 2014-03-27
    相关资源
    最近更新 更多