IBM MobileFirst/Worklight JSON
IBM MobileFirst/Worklight JSON
在 IBM MobileFirst 7.0 中,我通过将 MobileFirst Platform Studio 指向本地 Web 服务器上的 WSDL 运行 创建了一个 SOAP 适配器。当调用 SOAP 适配器时,它将 JSON 转换为 XML,然后当从 XML 接收到返回 JSON 的响应时,它设置数字(整数、小数)和布尔值JSON returned 中的值作为字符串,即在响应中用双引号将它们括起来,而不是不加引号。我没有更改 MFP Studio 自动生成的适配器中的任何内容。据我了解,在 JSON 中,不引用布尔值和数字以及空值。我还有一个非 MobileFirst Web 服务器 运行,通过 Spring 启动,使用 WebServiceGatewaySupport
调用相同的 SOAP 服务和 @ResponseBody
注释使其自动 return JSON 并且数字和布尔值不在字符串 returned 中的双引号中。
是否期望 MobileFirst/Worklight return 在 JSON 体内将所有内容都作为 String
?如果是这样,为什么?如果没有,关于我可能需要做些什么才能避免这种情况发生并使数字和布尔值不被引用有什么想法吗?
下面是 WSDL 和 JSON request/responses to/from Worklight SOAP 适配器的相关部分。
<xs:element name="customerFoodOrderRequest">
<xs:complexType>
<xs:sequence>
<xs:element name="customerId" type="xs:int"/>
<xs:element name="dateTime" type="xs:dateTime"/>
<xs:element name="item" type="xs:string"/>
<xs:element name="amount" type="tns:money"/>
</xs:sequence>
</xs:complexType>
</xs:element>
<xs:simpleType name="money">
<xs:restriction base="xs:decimal">
<xs:fractionDigits value="2"/>
</xs:restriction>
</xs:simpleType>
<xs:element name="customerFoodOrderResponse">
<xs:complexType>
<xs:sequence>
<xs:element name="success" type="xs:boolean"/>
</xs:sequence>
</xs:complexType>
</xs:element>
<xs:element name="getRecentFoodOrdersForCustomerRequest">
<xs:complexType>
<xs:sequence>
<xs:element name="customerId" type="xs:int"/>
</xs:sequence>
</xs:complexType>
</xs:element>
<xs:element name="getRecentFoodOrdersForCustomerResponse">
<xs:complexType>
<xs:sequence maxOccurs="unbounded" minOccurs="0">
<xs:element name="foodOrders" type="tns:foodOrder"/>
</xs:sequence>
</xs:complexType>
</xs:element>
<xs:complexType name="foodOrder">
<xs:sequence>
<xs:element name="dateTime" type="xs:dateTime"/>
<xs:element name="item" type="xs:string"/>
<xs:element name="amount" type="tns:money"/>
</xs:sequence>
</xs:complexType>
JSON 响应 #1(amount
是一个 xs:decimal 包裹在一个名为 money
的 SimpleType 中,但它被输出到 JSON 作为字符串)
{
"Envelope": {
"Body": {
"getRecentFoodOrdersForCustomerResponse": {
"foodOrders": {
"amount": "10",
"dateTime": "2015-02-14T12:00:00.123-07:00",
"item": "MyItem"
},
"ns2": "http:\/\/company.com\/demo\/demo-producing-web-service"
}
},
"Header": "",
"SOAP-ENV": "http:\/\/schemas.xmlsoap.org\/soap\/envelope\/"
},
"errors": [
],
"info": [
],
"isSuccessful": true,
"responseHeaders": {
"Accept": "text\/xml, text\/html, image\/gif, image\/jpeg, *; q=.2, *\/*; q=.2",
"Content-Length": "444",
"Content-Type": "text\/xml;charset=utf-8",
"Date": "Wed, 20 May 2015 02:58:26 GMT",
"SOAPAction": "\"\"",
"Server": "Apache-Coyote\/1.1"
},
"responseTime": 328,
"statusCode": 200,
"statusReason": "OK",
"totalTime": 359,
"warnings": [
]
}
JSON 响应 #2,双引号中 success
的布尔值:
{
"Envelope": {
"Body": {
"customerFoodOrderResponse": {
"ns2": "http:\/\/company.com\/demo\/demo-producing-web-service",
"success": "true"
}
},
"Header": "",
"SOAP-ENV": "http:\/\/schemas.xmlsoap.org\/soap\/envelope\/"
},
"errors": [
],
"info": [
],
"isSuccessful": true,
"responseHeaders": {
"Accept": "text\/xml, text\/html, image\/gif, image\/jpeg, *; q=.2, *\/*; q=.2",
"Content-Length": "304",
"Content-Type": "text\/xml;charset=utf-8",
"Date": "Wed, 20 May 2015 02:46:02 GMT",
"SOAPAction": "\"\"",
"Server": "Apache-Coyote\/1.1"
},
"responseTime": 764,
"statusCode": 200,
"statusReason": "OK",
"totalTime": 796,
"warnings": [
]
}
JSON 响应 #3(客户 ID 用双引号引起来,即使它被定义为 xs:int)
"customer": {
"accountNumber": "11127174",
"firstName": "JOHN",
"id": "200",
"lastName": "DOE",
"status": "None"
},
来自 WSDL 的客户定义
<xs:complexType name="customer">
<xs:sequence>
<xs:element name="id" type="xs:int"/>
<xs:element minOccurs="1" name="firstName" nillable="false" type="xs:string"/>
<xs:element minOccurs="1" name="lastName" nillable="false" type="xs:string"/>
<xs:element minOccurs="1" name="status" nillable="false" type="tns:status"/>
<xs:element name="accountNumber" type="xs:string"/>
</xs:sequence>
</xs:complexType>
Yes all the responses from Worklight adapter will be converted to
JSON before reaching the Client device (Mobile,Web browser,etc)
要更清楚地了解适配器的结构和工作方式,请阅读下面的 link Overview of worklight Adapters
接下来为什么Json总是比XML?
- JSON 比 XML 有几个优点。它要小得多,也不那么臃肿,所以你通过网络传输的数据要少得多——这在移动设备的情况下会有很大的不同。
- 它在javascript代码中也更容易使用,因为您可以简单地将数据包直接传递到javascript数组中,而不需要任何解析、提取和转换,所以它要少得多CPU 也很密集。
这些是需要考虑的一些要点,还有关于此的更多详细信息consider this link以及google您自己以获取更多信息。
备注
- 您可以根据需要使用 xsl 转换 json,但我建议您使用 parseInt 而不是那个,当您阅读 customer.accountNumber 时,它会给您 11127174 而不是“11127174” " 所以用 JAVASCRIPT 方式比用 xsl
更容易处理
- 我们无法更改工作灯的行为,但您可以将 xml 更改为您自己的 JSON 格式,方法是使用 XSL 将 xml 转换为 JSON 格式,如下所示你希望没有引号等
在 IBM MobileFirst 7.0 中,我通过将 MobileFirst Platform Studio 指向本地 Web 服务器上的 WSDL 运行 创建了一个 SOAP 适配器。当调用 SOAP 适配器时,它将 JSON 转换为 XML,然后当从 XML 接收到返回 JSON 的响应时,它设置数字(整数、小数)和布尔值JSON returned 中的值作为字符串,即在响应中用双引号将它们括起来,而不是不加引号。我没有更改 MFP Studio 自动生成的适配器中的任何内容。据我了解,在 JSON 中,不引用布尔值和数字以及空值。我还有一个非 MobileFirst Web 服务器 运行,通过 Spring 启动,使用 WebServiceGatewaySupport
调用相同的 SOAP 服务和 @ResponseBody
注释使其自动 return JSON 并且数字和布尔值不在字符串 returned 中的双引号中。
是否期望 MobileFirst/Worklight return 在 JSON 体内将所有内容都作为 String
?如果是这样,为什么?如果没有,关于我可能需要做些什么才能避免这种情况发生并使数字和布尔值不被引用有什么想法吗?
下面是 WSDL 和 JSON request/responses to/from Worklight SOAP 适配器的相关部分。
<xs:element name="customerFoodOrderRequest">
<xs:complexType>
<xs:sequence>
<xs:element name="customerId" type="xs:int"/>
<xs:element name="dateTime" type="xs:dateTime"/>
<xs:element name="item" type="xs:string"/>
<xs:element name="amount" type="tns:money"/>
</xs:sequence>
</xs:complexType>
</xs:element>
<xs:simpleType name="money">
<xs:restriction base="xs:decimal">
<xs:fractionDigits value="2"/>
</xs:restriction>
</xs:simpleType>
<xs:element name="customerFoodOrderResponse">
<xs:complexType>
<xs:sequence>
<xs:element name="success" type="xs:boolean"/>
</xs:sequence>
</xs:complexType>
</xs:element>
<xs:element name="getRecentFoodOrdersForCustomerRequest">
<xs:complexType>
<xs:sequence>
<xs:element name="customerId" type="xs:int"/>
</xs:sequence>
</xs:complexType>
</xs:element>
<xs:element name="getRecentFoodOrdersForCustomerResponse">
<xs:complexType>
<xs:sequence maxOccurs="unbounded" minOccurs="0">
<xs:element name="foodOrders" type="tns:foodOrder"/>
</xs:sequence>
</xs:complexType>
</xs:element>
<xs:complexType name="foodOrder">
<xs:sequence>
<xs:element name="dateTime" type="xs:dateTime"/>
<xs:element name="item" type="xs:string"/>
<xs:element name="amount" type="tns:money"/>
</xs:sequence>
</xs:complexType>
JSON 响应 #1(amount
是一个 xs:decimal 包裹在一个名为 money
的 SimpleType 中,但它被输出到 JSON 作为字符串)
{
"Envelope": {
"Body": {
"getRecentFoodOrdersForCustomerResponse": {
"foodOrders": {
"amount": "10",
"dateTime": "2015-02-14T12:00:00.123-07:00",
"item": "MyItem"
},
"ns2": "http:\/\/company.com\/demo\/demo-producing-web-service"
}
},
"Header": "",
"SOAP-ENV": "http:\/\/schemas.xmlsoap.org\/soap\/envelope\/"
},
"errors": [
],
"info": [
],
"isSuccessful": true,
"responseHeaders": {
"Accept": "text\/xml, text\/html, image\/gif, image\/jpeg, *; q=.2, *\/*; q=.2",
"Content-Length": "444",
"Content-Type": "text\/xml;charset=utf-8",
"Date": "Wed, 20 May 2015 02:58:26 GMT",
"SOAPAction": "\"\"",
"Server": "Apache-Coyote\/1.1"
},
"responseTime": 328,
"statusCode": 200,
"statusReason": "OK",
"totalTime": 359,
"warnings": [
]
}
JSON 响应 #2,双引号中 success
的布尔值:
{
"Envelope": {
"Body": {
"customerFoodOrderResponse": {
"ns2": "http:\/\/company.com\/demo\/demo-producing-web-service",
"success": "true"
}
},
"Header": "",
"SOAP-ENV": "http:\/\/schemas.xmlsoap.org\/soap\/envelope\/"
},
"errors": [
],
"info": [
],
"isSuccessful": true,
"responseHeaders": {
"Accept": "text\/xml, text\/html, image\/gif, image\/jpeg, *; q=.2, *\/*; q=.2",
"Content-Length": "304",
"Content-Type": "text\/xml;charset=utf-8",
"Date": "Wed, 20 May 2015 02:46:02 GMT",
"SOAPAction": "\"\"",
"Server": "Apache-Coyote\/1.1"
},
"responseTime": 764,
"statusCode": 200,
"statusReason": "OK",
"totalTime": 796,
"warnings": [
]
}
JSON 响应 #3(客户 ID 用双引号引起来,即使它被定义为 xs:int)
"customer": {
"accountNumber": "11127174",
"firstName": "JOHN",
"id": "200",
"lastName": "DOE",
"status": "None"
},
来自 WSDL 的客户定义
<xs:complexType name="customer">
<xs:sequence>
<xs:element name="id" type="xs:int"/>
<xs:element minOccurs="1" name="firstName" nillable="false" type="xs:string"/>
<xs:element minOccurs="1" name="lastName" nillable="false" type="xs:string"/>
<xs:element minOccurs="1" name="status" nillable="false" type="tns:status"/>
<xs:element name="accountNumber" type="xs:string"/>
</xs:sequence>
</xs:complexType>
Yes all the responses from Worklight adapter will be converted to JSON before reaching the Client device (Mobile,Web browser,etc)
要更清楚地了解适配器的结构和工作方式,请阅读下面的 link Overview of worklight Adapters
接下来为什么Json总是比XML?
- JSON 比 XML 有几个优点。它要小得多,也不那么臃肿,所以你通过网络传输的数据要少得多——这在移动设备的情况下会有很大的不同。
- 它在javascript代码中也更容易使用,因为您可以简单地将数据包直接传递到javascript数组中,而不需要任何解析、提取和转换,所以它要少得多CPU 也很密集。
这些是需要考虑的一些要点,还有关于此的更多详细信息consider this link以及google您自己以获取更多信息。
备注
- 您可以根据需要使用 xsl 转换 json,但我建议您使用 parseInt 而不是那个,当您阅读 customer.accountNumber 时,它会给您 11127174 而不是“11127174” " 所以用 JAVASCRIPT 方式比用 xsl 更容易处理
- 我们无法更改工作灯的行为,但您可以将 xml 更改为您自己的 JSON 格式,方法是使用 XSL 将 xml 转换为 JSON 格式,如下所示你希望没有引号等