如何在wso2 EI中将xml响应转换为JSON数组

qjp7pelc  于 2022-12-05  发布在  其他
关注(0)|答案(1)|浏览(127)

我得到了一个XML响应,需要将其转换为JSON数组。
XML响应如下:

<jsonObject>
<message>
    <status>Success</status>
    <timestam>2022-12-04T17:51:15.9841813+11:00</timestam>
    <resultCount>35</resultCount>
    <totalCount>35</totalCount>
    <fields
        xmlns="htt://ws.aache.org/ns/synase">
        <AlicationNumber>
            <listItems>
                <text>97282A08</text>
                <value>97282A08</value>
            </listItems>
        </AlicationNumber>
    </fields>
    <fields
        xmlns="htt://ws.aache.org/ns/synase">
        <AlicationNumber>
            <listItems>
                <text>185804A09</text>
                <value>185804A09</value>
            </listItems>
        </AlicationNumber>
    </fields>
    <fields
        xmlns="htt://ws.aache.org/ns/synase">
        <AlicationNumber>
            <listItems>
                <text>241248A09</text>
                <value>241248A09</value>
            </listItems>
        </AlicationNumber>
    </fields>
    <fields
        xmlns="htt://ws.aache.org/ns/synase">
        <AlicationNumber>
            <listItems>
                <text>258111A09</text>
                <value>258111A09</value>
            </listItems>
        </AlicationNumber>
    </fields>
    <fields
        xmlns="htt://ws.aache.org/ns/synase">
        <AlicationNumber>
            <listItems>
                <text>429398A11</text>
                <value>429398A11</value>
            </listItems>
        </AlicationNumber>
    </fields>
    <fields
        xmlns="htt://ws.aache.org/ns/synase">
        <AlicationNumber>
            <listItems>
                <text>450962A11</text>
                <value>450962A11</value>
            </listItems>
        </AlicationNumber>
    </fields>
    <fields
        xmlns="htt://ws.aache.org/ns/synase">
        <AlicationNumber>
            <listItems>
                <text>588602A12</text>
                <value>588602A12</value>
            </listItems>
        </AlicationNumber>
    </fields>
    <fields
        xmlns="htt://ws.aache.org/ns/synase">
        <AlicationNumber>
            <listItems>
                <text>618329A12</text>
                <value>618329A12</value>
            </listItems>
        </AlicationNumber>
    </fields>
    <fields
        xmlns="htt://ws.aache.org/ns/synase">
        <AlicationNumber>
            <listItems>
                <text>624645A12</text>
                <value>624645A12</value>
            </listItems>
        </AlicationNumber>
    </fields>
    <fields
        xmlns="htt://ws.aache.org/ns/synase">
        <AlicationNumber>
            <listItems>
                <text>643029A12</text>
                <value>643029A12</value>
            </listItems>
        </AlicationNumber>
    </fields>
    <fields
        xmlns="htt://ws.aache.org/ns/synase">
        <AlicationNumber>
            <listItems>
                <text>655593A12</text>
                <value>655593A12</value>
            </listItems>
        </AlicationNumber>
    </fields>
    <fields
        xmlns="htt://ws.aache.org/ns/synase">
        <AlicationNumber>
            <listItems>
                <text>684292A12</text>
                <value>684292A12</value>
            </listItems>
        </AlicationNumber>
    </fields>
    <fields
        xmlns="htt://ws.aache.org/ns/synase">
        <AlicationNumber>
            <listItems>
                <text>903240A14</text>
                <value>903240A14</value>
            </listItems>
        </AlicationNumber>
    </fields>
    <fields
        xmlns="htt://ws.aache.org/ns/synase">
        <AlicationNumber>
            <listItems>
                <text>1031807A15</text>
                <value>1031807A15</value>
            </listItems>
        </AlicationNumber>
    </fields>
    <fields
        xmlns="htt://ws.aache.org/ns/synase">
        <AlicationNumber>
            <listItems>
                <text>1353624A17</text>
                <value>1353624A17</value>
            </listItems>
        </AlicationNumber>
    </fields>
    <fields
        xmlns="htt://ws.aache.org/ns/synase">
        <AlicationNumber>
            <listItems>
                <text>1353626A17</text>
                <value>1353626A17</value>
            </listItems>
        </AlicationNumber>
    </fields>
    <fields
        xmlns="htt://ws.aache.org/ns/synase">
        <AlicationNumber>
            <listItems>
                <text>1436375A18</text>
                <value>1436375A18</value>
            </listItems>
        </AlicationNumber>
    </fields>
    <fields
        xmlns="htt://ws.aache.org/ns/synase">
        <AlicationNumber>
            <listItems>
                <text>1455356A18</text>
                <value>1455356A18</value>
            </listItems>
        </AlicationNumber>
    </fields>
    <fields
        xmlns="htt://ws.aache.org/ns/synase">
        <AlicationNumber>
            <listItems>
                <text>1500185A18</text>
                <value>1500185A18</value>
            </listItems>
        </AlicationNumber>
    </fields>
    <fields
        xmlns="htt://ws.aache.org/ns/synase">
        <AlicationNumber>
            <listItems>
                <text>1511985A18</text>
                <value>1511985A18</value>
            </listItems>
        </AlicationNumber>
    </fields>
    <fields
        xmlns="htt://ws.aache.org/ns/synase">
        <AlicationNumber>
            <listItems>
                <text>1625059A19</text>
                <value>1625059A19</value>
            </listItems>
        </AlicationNumber>
    </fields>
    <fields
        xmlns="htt://ws.aache.org/ns/synase">
        <AlicationNumber>
            <listItems>
                <text>1630914A19</text>
                <value>1630914A19</value>
            </listItems>
        </AlicationNumber>
    </fields>
    <fields
        xmlns="htt://ws.aache.org/ns/synase">
        <AlicationNumber>
            <listItems>
                <text>1741745A20</text>
                <value>1741745A20</value>
            </listItems>
        </AlicationNumber>
    </fields>
    <fields
        xmlns="htt://ws.aache.org/ns/synase">
        <AlicationNumber>
            <listItems>
                <text>1878082A21</text>
                <value>1878082A21</value>
            </listItems>
        </AlicationNumber>
    </fields>
    <fields
        xmlns="htt://ws.aache.org/ns/synase">
        <AlicationNumber>
            <listItems>
                <text>2061825A22</text>
                <value>2061825A22</value>
            </listItems>
        </AlicationNumber>
    </fields>
    <fields
        xmlns="htt://ws.aache.org/ns/synase">
        <AlicationNumber>
            <listItems>
                <text>2061829A22</text>
                <value>2061829A22</value>
            </listItems>
        </AlicationNumber>
    </fields>
    <fields
        xmlns="htt://ws.aache.org/ns/synase">
        <AlicationNumber>
            <listItems>
                <text>2061830A22</text>
                <value>2061830A22</value>
            </listItems>
        </AlicationNumber>
    </fields>
    <fields
        xmlns="htt://ws.aache.org/ns/synase">
        <AlicationNumber>
            <listItems>
                <text>5067/1993</text>
                <value>5067/1993</value>
            </listItems>
        </AlicationNumber>
    </fields>
    <fields
        xmlns="htt://ws.aache.org/ns/synase">
        <AlicationNumber>
            <listItems>
                <text>497/1998</text>
                <value>497/1998</value>
            </listItems>
        </AlicationNumber>
    </fields>
    <fields
        xmlns="htt://ws.aache.org/ns/synase">
        <AlicationNumber>
            <listItems>
                <text>954/1998</text>
                <value>954/1998</value>
            </listItems>
        </AlicationNumber>
    </fields>
    <fields
        xmlns="htt://ws.aache.org/ns/synase">
        <AlicationNumber>
            <listItems>
                <text>206A09</text>
                <value>206A09</value>
            </listItems>
        </AlicationNumber>
    </fields>
    <fields
        xmlns="htt://ws.aache.org/ns/synase">
        <AlicationNumber>
            <listItems>
                <text>34A09</text>
                <value>34A09</value>
            </listItems>
        </AlicationNumber>
    </fields>
    <fields
        xmlns="htt://ws.aache.org/ns/synase">
        <AlicationNumber>
            <listItems>
                <text>15187A09</text>
                <value>15187A09</value>
            </listItems>
        </AlicationNumber>
    </fields>
    <fields
        xmlns="htt://ws.aache.org/ns/synase">
        <AlicationNumber>
            <listItems>
                <text>15188A09</text>
                <value>15188A09</value>
            </listItems>
        </AlicationNumber>
    </fields>
    <fields
        xmlns="htt://ws.aache.org/ns/synase">
        <AlicationNumber>
            <listItems>
                <text>18122A04</text>
                <value>18122A04</value>
            </listItems>
        </AlicationNumber>
    </fields>
</message>

我使用messageType“application/JSON”,得到的结果如下:

{
"message": {
    "status": "Success",
    "timestam": "2022-12-04T17:51:15.9841813+11:00",
    "resultCount": 35,
    "totalCount": 35,
    "fields": [
        {
            "AlicationNumber": {
                "listItems": {
                    "text": "97282A08",
                    "value": "97282A08"
                }
            }
        },
        {
            "AlicationNumber": {
                "listItems": {
                    "text": "185804A09",
                    "value": "185804A09"
                }
            }
        },
        {
            "AlicationNumber": {
                "listItems": {
                    "text": "241248A09",
                    "value": "241248A09"
                }
            }
        },
        {
            "AlicationNumber": {
                "listItems": {
                    "text": "258111A09",
                    "value": "258111A09"
                }
            }
        },
        {
            "AlicationNumber": {
                "listItems": {
                    "text": "429398A11",
                    "value": "429398A11"
                }
            }
        },
        {
            "AlicationNumber": {
                "listItems": {
                    "text": "450962A11",
                    "value": "450962A11"
                }
            }
        },
        {
            "AlicationNumber": {
                "listItems": {
                    "text": "588602A12",
                    "value": "588602A12"
                }
            }
        },
        {
            "AlicationNumber": {
                "listItems": {
                    "text": "618329A12",
                    "value": "618329A12"
                }
            }
        },
        {
            "AlicationNumber": {
                "listItems": {
                    "text": "624645A12",
                    "value": "624645A12"
                }
            }
        },
        {
            "AlicationNumber": {
                "listItems": {
                    "text": "643029A12",
                    "value": "643029A12"
                }
            }
        },
        {
            "AlicationNumber": {
                "listItems": {
                    "text": "655593A12",
                    "value": "655593A12"
                }
            }
        },
        {
            "AlicationNumber": {
                "listItems": {
                    "text": "684292A12",
                    "value": "684292A12"
                }
            }
        },
        {
            "AlicationNumber": {
                "listItems": {
                    "text": "903240A14",
                    "value": "903240A14"
                }
            }
        },
        {
            "AlicationNumber": {
                "listItems": {
                    "text": "1031807A15",
                    "value": "1031807A15"
                }
            }
        },
        {
            "AlicationNumber": {
                "listItems": {
                    "text": "1353624A17",
                    "value": "1353624A17"
                }
            }
        },
        {
            "AlicationNumber": {
                "listItems": {
                    "text": "1353626A17",
                    "value": "1353626A17"
                }
            }
        },
        {
            "AlicationNumber": {
                "listItems": {
                    "text": "1436375A18",
                    "value": "1436375A18"
                }
            }
        },
        {
            "AlicationNumber": {
                "listItems": {
                    "text": "1455356A18",
                    "value": "1455356A18"
                }
            }
        },
        {
            "AlicationNumber": {
                "listItems": {
                    "text": "1500185A18",
                    "value": "1500185A18"
                }
            }
        },
        {
            "AlicationNumber": {
                "listItems": {
                    "text": "1511985A18",
                    "value": "1511985A18"
                }
            }
        },
        {
            "AlicationNumber": {
                "listItems": {
                    "text": "1625059A19",
                    "value": "1625059A19"
                }
            }
        },
        {
            "AlicationNumber": {
                "listItems": {
                    "text": "1630914A19",
                    "value": "1630914A19"
                }
            }
        },
        {
            "AlicationNumber": {
                "listItems": {
                    "text": "1741745A20",
                    "value": "1741745A20"
                }
            }
        },
        {
            "AlicationNumber": {
                "listItems": {
                    "text": "1878082A21",
                    "value": "1878082A21"
                }
            }
        },
        {
            "AlicationNumber": {
                "listItems": {
                    "text": "2061825A22",
                    "value": "2061825A22"
                }
            }
        },
        {
            "AlicationNumber": {
                "listItems": {
                    "text": "2061829A22",
                    "value": "2061829A22"
                }
            }
        },
        {
            "AlicationNumber": {
                "listItems": {
                    "text": "2061830A22",
                    "value": "2061830A22"
                }
            }
        },
        {
            "AlicationNumber": {
                "listItems": {
                    "text": "5067/1993",
                    "value": "5067/1993"
                }
            }
        },
        {
            "AlicationNumber": {
                "listItems": {
                    "text": "497/1998",
                    "value": "497/1998"
                }
            }
        },
        {
            "AlicationNumber": {
                "listItems": {
                    "text": "954/1998",
                    "value": "954/1998"
                }
            }
        },
        {
            "AlicationNumber": {
                "listItems": {
                    "text": "206A09",
                    "value": "206A09"
                }
            }
        },
        {
            "AlicationNumber": {
                "listItems": {
                    "text": "34A09",
                    "value": "34A09"
                }
            }
        },
        {
            "AlicationNumber": {
                "listItems": {
                    "text": "15187A09",
                    "value": "15187A09"
                }
            }
        },
        {
            "AlicationNumber": {
                "listItems": {
                    "text": "15188A09",
                    "value": "15188A09"
                }
            }
        },
        {
            "AlicationNumber": {
                "listItems": {
                    "text": "18122A04",
                    "value": "18122A04"
                }
            }
        }
    ]
}

}
但我需要得到的格式如下:

{
"message": {
    "status": "Success",
    "timestam": "2022-12-04T17:51:15.9841813+11:00",
    "resultCount": 35,
    "totalCount": 35,
    "fields": [
        {
            "AlicationNumber": {
                "listItems": [
                {
                    "text": "97282A08",
                    "value": "97282A08"
                },
                {
                    "text": "185804A09",
                    "value": "185804A09"
                },
                {
                    "text": "241248A09",
                    "value": "241248A09"
                },
                {"text": "258111A09",
                    "value": "258111A09"
            },
            ...
        ]
        }
        
        }
        ]
        }}

我也使用了JSON转换器,但是没有改变响应格式。

{
    "$schema": "http://json-schema.org/draft-04/schema#",
    "type": "object",
    "properties": {
        "fields": {
            "type": "object",
            "properties": {
                "ApplicationNumber": {
                    "type": "object"
                },
                "listItems":{
                     "type": "Array",
                      "properties":{
                        
                                "text":{
                                    "type":"string"
                                },
                                "value":{
                                    "type":"string"
                                }
                            }
                        }
                        
                    }
                }
            }
        }

你能帮助我如何实现这一点吗?我正在使用wso 2集成工作室7.2。

6gpjuf90

6gpjuf901#

你可以使用PayloadFactory Mediator来完成这个任务,看看下面的例子。

<payloadFactory media-type="json">
      <format>{
          "message": {
              "status": "$1",
              "timestam": "$2",
              "resultCount": $3,
              "totalCount": $4,
              "fields": [
                  {
                      "AlicationNumber": $5 
                  }
                  ]
            }}
      </format>
      <args>
        <arg evaluator="xml" expression="//message/status" />
        <arg evaluator="xml" expression="//message/timestam" />
        <arg evaluator="xml" expression="//message/resultCount" />
        <arg evaluator="xml" expression="//message/totalCount" />
        <arg evaluator="xml" expression="//sy:AlicationNumber/sy:listItems" xmlns:sy="htt://ws.aache.org/ns/synase"/>
      </args>
</payloadFactory>

完整的API

<?xml version="1.0" encoding="UTF-8"?>
<api context="/jsontest" name="HelloWorld" xmlns="http://ws.apache.org/ns/synapse">
    <resource methods="POST">
        <inSequence>
            <payloadFactory media-type="json">
                <format>{
                    "message": {
                        "status": "$1",
                        "timestam": "$2",
                        "resultCount": $3,
                        "totalCount": $4,
                        "fields": [
                            {
                                "AlicationNumber": $5 
                            }
                            ]
                         }}
                </format>
                <args>
                    <arg evaluator="xml" expression="//message/status"/>
                    <arg evaluator="xml" expression="//message/timestam"/>
                    <arg evaluator="xml" expression="//message/resultCount"/>
                    <arg evaluator="xml" expression="//message/totalCount"/>
                    <arg evaluator="xml" expression="//sy:AlicationNumber/sy:listItems" xmlns:sy="htt://ws.aache.org/ns/synase"/>
                </args>
            </payloadFactory>
            <log category="DEBUG" level="full"/>
            <respond/>
        </inSequence>
        <outSequence/>
        <faultSequence/>
    </resource>
</api>

鲁伊斯特

curl --location --request POST 'http://localhost:8290/jsontest' \
--header 'Content-Type: application/xml' \
--data-raw '<jsonObject>
    <message>
        <status>Success</status>
        <timestam>2022-12-04T17:51:15.9841813+11:00</timestam>
        <resultCount>35</resultCount>
        <totalCount>35</totalCount>
        <fields
        xmlns="htt://ws.aache.org/ns/synase">
            <AlicationNumber>
                <listItems>
                    <text>97282A08</text>
                    <value>97282A08</value>
                </listItems>
            </AlicationNumber>
        </fields>
        <fields
        xmlns="htt://ws.aache.org/ns/synase">
            <AlicationNumber>
                <listItems>
                    <text>185804A09</text>
                    <value>185804A09</value>
                </listItems>
            </AlicationNumber>
        </fields>
        <fields
        xmlns="htt://ws.aache.org/ns/synase">
            <AlicationNumber>
                <listItems>
                    <text>241248A09</text>
                    <value>241248A09</value>
                </listItems>
            </AlicationNumber>
        </fields>
    </message>
</jsonObject>'

回应

{
    "message": {
        "status": "Success",
        "timestam": "2022-12-04T17:51:15.9841813+11:00",
        "resultCount": 35,
        "totalCount": 35,
        "fields": [
            {
                "AlicationNumber": {
                    "listItems": [
                        {
                            "text": "97282A08",
                            "value": "97282A08"
                        },
                        {
                            "text": "185804A09",
                            "value": "185804A09"
                        },
                        {
                            "text": "241248A09",
                            "value": "241248A09"
                        }
                    ]
                }
            }
        ]
    }
}

相关问题