Java Action

1
Hello I have a list in Mendix of the following format: {"value1": 5, "value2": "test", "value3": 55} {"value1": 2, "value2": "test2", "value3": 5} .... I would like to store the individual values for value1 etc. in a separate list. Ideally, the list should be named accordingly. I was planning to do this with a JavaAction with the following code:     package myfirstmodule.actions; import com.mendix.core.Core; import com.mendix.systemwideinterfaces.core.IMendixObject; import com.mendix.systemwideinterfaces.core.IContext; import java.util.ArrayList; import java.util.List; import com.mendix.webui.CustomJavaAction; import org.json.JSONArray; import org.json.JSONException; import org.json.JSONObject; public class FormatArray extends CustomJavaAction<java.util.List<IMendixObject>> {     private java.util.List<IMendixObject> __Parameter;     private java.util.List<myfirstmodule.proxies.ts_string> Parameter;     public FormatArray(IContext context, java.util.List<IMendixObject> Parameter) {         super(context);         this.__Parameter = Parameter;     }     @java.lang.Override     public java.util.List<IMendixObject> executeAction() throws Exception {         this.Parameter = java.util.Optional.ofNullable(this.__Parameter)             .orElse(java.util.Collections.emptyList())             .stream()             .map(__ParameterElement -> myfirstmodule.proxies.ts_string.initialize(getContext(), __ParameterElement))             .collect(java.util.stream.Collectors.toList());         // BEGIN USER CODE         List<Integer> value1List = new ArrayList<>();         List<String> value2List = new ArrayList<>();         List<Integer> value3List = new ArrayList<>();         for (myfirstmodule.proxies.ts_string obj : Parameter) {             try {                 JSONObject jsonObject = new JSONObject(obj.getJsonString());                 int value1 = jsonObject.getInt("value1");                 String value2 = jsonObject.getString("value2");                 int value3 = jsonObject.getInt("value3");                 value1List.add(value1);                 value2List.add(value2);                 value3List.add(value3);             } catch (JSONException e) {                 // Handle JSON parsing exception                 e.printStackTrace();             }         }         // END USER CODE         return null; // Gib hier das Ergebnis deiner Verarbeitung zurück     }     /**      * Returns a string representation of this action      * @return a string representation of this action      */     @java.lang.Override     public java.lang.String toString() {         return "FormatArray";     }     // BEGIN EXTRA CODE     // END EXTRA CODE }   I get the error that the library org.json cannot be resolved. Can this work with the code? How can I add the library? It did not work via the following link: https://github.com/stleary/JSON-java
asked
4 answers
0

Verify the userlib folder to see whether any appropriate jars exist. most probably this 

answered
0

Why do you want to do this in a Java action? You can do this in a microflow.

 

[EDIT]

  1. Map your JSON structure to a Mendix domain model using Import mapping
  2. Create another entity with the attributes you need to store your data
  3. Loop over the resulting list after the import using a loop activity and for each object create a new object and fill the attributes with the value from the imported object.
  4. Create a new list before the loop and add each newly created object to it.

Finally commit your new list to store it in your database.

answered
0

How do I use Import Mapping. I have a list with these values:
{"value1": 5, "value2": "test", "value3": 55}
{"value1": 2, "value2": "test2", "value3": 5}
.…

But I cant choose the list under “mapping” 

I included an entity with value1, value2, value3 named Databank, I also cant choose this entity under Mapping
I need more specific instructions

answered
0

Hi Sophia,

 

You can use JSON structure for this.

 

- create Json structure with your mapping values:

actual values:{"value1": 5, "value2": "test", "value3": 55}
                          {"value1": 2, "value2": "test2", "value3": 5}

JSON Structre: {
  "data": [
    {
      "value1": 5,
      "value2": "test",
      "value3": 55
    },
    {
      "value1": 2,
      "value2": "test2",
      "value3": 5
    }
  ]
}

 

-create import mapping with above JSON file.

-Map entity in the Import, your data will get store in the entity.

 

if you required any logical help let me know.

 

Thank you

answered