0

Can someone give an example of how to save String in QuestionnaireItemOptionComponent? I can't find the way to do that. Is there any other way to do this? I have options stored in JSONArray.

This is what I'm trying to do:

JSONArray options = rec.getJSONArray("options");

QuestionnaireItemOptionComponent test = new QuestionnaireItemOptionComponent();
            List<QuestionnaireItemOptionComponent> listdata = new ArrayList<>();
            if (options != null) {
                for (int j=0;j<options.length();j++){
                    //test = options.getString(j); // String to QuestionnaireItemOptionComponent 
                    //test.setValue(options.getString(j)) // String to QuestionnaireItemOptionComponent 
                    listdata.add(test);
                }
            }

And this is the result I would like to get:

"option": [
     {"valueString" : "value_1"},
     {"valueString" : "value_2"},
     {"valueString" : "valie_3"},
     {"valueString" : "value_4"},
     ...
     {"valueString" : "value_n"}
]

Dependencies I'm using are next:

<dependency>
   <groupId>ca.uhn.hapi.fhir</groupId>
   <artifactId>hapi-fhir-structures-dstu3</artifactId>
   <version>5.1.0</version>
</dependency>
<dependency>
   <groupId>ca.uhn.hapi.fhir</groupId>
   <artifactId>hapi-fhir-validation-resources-dstu3</artifactId>
   <version>5.1.0</version>
</dependency>
<dependency>
    <groupId>ca.uhn.hapi.fhir</groupId>
    <artifactId>hapi-fhir-structures-r4</artifactId>
    <version>5.1.0</version>
</dependency>
<dependency>
    <groupId>ca.uhn.hapi.fhir</groupId>
    <artifactId>hapi-fhir-base</artifactId>
    <version>5.1.0</version>
</dependency>
SP.
  • 322
  • 5
  • 19

2 Answers2

0

Try:

test.addChild("valueString").setValue(options.getString(j))

Lloyd McKenzie
  • 6,345
  • 1
  • 13
  • 10
  • This is the way I would like to go with, but `.setValue()` option is not available after `.addChild()`. I've updated the original question. Am I missing some dependency or what? – SP. Mar 04 '21 at 08:50
0

OK, I found a way how to do this.

Here is code:

JSONArray options = rec.getJSONArray("options");
QuestionnaireItemOptionComponent questionnaire_item = new QuestionnaireItemOptionComponent();
List<QuestionnaireItemOptionComponent> questionnaire_item_list = new ArrayList<>();
StringType stringType = new StringType();
if (options != null) {
    for (int j = 0; j < options.length(); j++) {
        stringType.setValue(options.getString(j));
        questionnaire_item.setValue(stringType);
        questionnaire_item_list.add(questionnaire_item);
    }
}

Questionnaire.QuestionnaireItemComponent questionnaire = new Questionnaire.QuestionnaireItemComponent();
questionnaire.setOption(questionnaire_item_list);

This is the final result of the option part inside the Questionnaire:

"option":[
   {
      "valueString":"Option 1"
   },
   {
      "valueString":"Option 2"
   },
   {
      "valueString":"Option 3"
   },
   {
      "valueString":"Option 4"
   }
]

While working with FHIR I had a problem understanding what's behind the Type value which setValue(Type value) is expecting as a parameter. This value can be any type like StringType, IntegerType and so on.

SP.
  • 322
  • 5
  • 19