简体   繁体   English

Grails 2.4.4绑定JSON嵌套对象

[英]Grails 2.4.4 binding JSON nested objects

I have a model with 3 classes, A, Parent and Child. 我有一个3类的模型,A,Parent和Child。 A hasMany Parent, and Child extends Parent. 一个hasMany Parent,而Child扩展Parent。

I need to handle complex logic on the UI to create the model, and decided to POST a JSON object with the same structure of my domain model. 我需要处理UI上的复杂逻辑来创建模型,并决定以与域模型相同的结构发布JSON对象。 Now, I have an instance of A that has some instances of Child associated, that in a javascript object on the client side. 现在,我有一个实例A,该实例具有与Child相关联的某些实例,这些实例位于客户端的javascript对象中。

When I POST the javascript object, serializing it to a JSON string, the controller receives it ok, then I do: def a = new A(request.JSON); 当我发布javascript对象并将其序列化为JSON字符串时,控制器会正常接收它,然后执行以下操作:def a = new A(request.JSON);

The A instance is created, but on the hasMany relationship it has instances of Parent instead of instances of Child, so I lose all the attribute values from Child. A实例已创建,但是在hasMany关系上,它具有Parent实例而不是Child实例,因此我丢失了Child的所有属性值。

How can I tell Grails to create instances of Child instead of instances of Parent? 如何告诉Grails创建Child实例而不是Parent实例?

UPDATE 更新

The request.JSON structure received by the controller looks like this: 控制器接收到的request.JSON结构如下所示:

[
 name: name,
 group: none,
 where: [
    [
        codeOperand: in_list,
        archetypeId: openEHR-EHR-EVALUATION.problem_diagnosis.v1,
        codeValues: [
            aaa,
            bbb
        ],
        path: /data[at0001]/items[at0005]/value,
        class: Child1,
        terminologyIdValues: [
            cie
        ],
        rmTypeName: DV_CODED_TEXT,
        terminologyIdOperand: eq
    ],
    [
        magnitudeValues: [
            1,
            22
        ],
        archetypeId: openEHR-EHR-OBSERVATION.blood_pressure.v1,
        path: /data[at0001]/events[at0006]/data[at0003]/items[at0004]/value,
        unitsOperand: eq,
        class: Child2,
        magnitudeOperand: between,
        rmTypeName: DV_QUANTITY,
        unitsValues: [
            mmHg
        ]
    ]
 ]
]

On this case I have 2 classes Child1 and Child2 that inherits from Parent. 在这种情况下,我有两个继承自Parent的类Child1和Child2。

I've added the "class" attribute to the objects because I read that might solve the problem but it didn't. 我已将“ class”属性添加到对象,因为我读到这可能会解决问题,但事实并非如此。

When I print the objects associated to the A instance, I got the right number of instances, but all the classes are "Parent", as you can see, some values are binded, but are all attributes of the Parent class. 当我打印与A实例关联的对象时,我得到了正确数量的实例,但是所有类都是“父”,如您所见,绑定了一些值,但都是Parent类的属性。

[
 (archetypeId: openEHR-EHR-EVALUATION.problem_diagnosis.v1,
  path: /data[at0001]/items[at0005]/value,
  rmTypeName: DV_CODED_TEXT,
  class: Parent),
 (archetypeId: openEHR-EHR-OBSERVATION.blood_pressure.v1,
  path: /data[at0001]/events[at0006]/data[at0003]/items[at0004]/value, 
  rmTypeName: DV_QUANTITY,
  class: Parent)
]

最后,我创建了遍历JSON对象并创建域实例的代码。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM